Search code examples
javamysqldoublejformattedtextfield

How to convert a decimal(6,2) value from database to a double value on a jformattedtextfield in this format **,**


i created a jformattedtextfield to accept only numbers:

NumberFormat f2 = NumberFormat.getNumberInstance(); 
txtformattedprice = new javax.swing.JFormattedTextField(f2);

Now i receive the value of the price from a table of my database and i want to put it in txtformattedprice:

rsriga.next(); //the code above is ok
txtformattedprice.setText(Double.toString((rsriga.getDouble("Price"))));

In my db the column "Price" is decimal(6,2) (example of a value: 14.15), if i run the application i get in txtformattedprice the value: 14.15 but i want 14,15 because if the user does a click in txtformattedprice and then he does a click in an other textfield, the value from 14.15 becomes 1.415 (now it's not a decimal). How i can i have in the txtformattedprice 14,15?


Solution

  • If I understand your question, you could use the JFormattedTextField#setValue(Object) which should use the NumberFormat you passed in to set the value. Something like,

    // You may not need the cast, not sure if rsriga returns double or Double
    txtformattedprice.setValue((Double) rsriga.getDouble("Price"));