Problems with using JFormattedTextField

P

Philipp

Hello,
I try to use a JFormattedTextField to show and enter numbers but can't
really figure out how to make it work.

The following code is the simplest example I have come up with.
If you start this program you get a window where you can enter a number
in the upper textfield and the lower should get update by multiplying
the upper number by 1.5.
Now do this: type 123.45 in the upper field and press return. the lower
field get 185.175 (correct). Now if you click on the lower field the
upper field gets truncated to just showing 123.
Why?
Maybe something to do with Locales?

Full code for example below.

Thanks for any suggestions
Karl

---- CODE ----

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestFrame extends JFrame implements PropertyChangeListener{
JTextField deltaXTf;
JTextField xCalTf;

public static void main(String[] args) {
TestFrame n = new TestFrame();
}

public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
deltaXTf = new
JFormattedTextField(NumberFormat.getNumberInstance());
xCalTf= new JFormattedTextField(NumberFormat.getNumberInstance());
deltaXTf.addPropertyChangeListener("value", this);
getContentPane().add(deltaXTf, BorderLayout.NORTH);
getContentPane().add(xCalTf, BorderLayout.SOUTH);
deltaXTf.setText("0");
xCalTf.setText("0");
pack();
setVisible(true);
}

public void propertyChange(PropertyChangeEvent evt) {
Object src = evt.getSource();
double deltaXTfVal = Double.parseDouble(deltaXTf.getText());
double newValue = deltaXTfVal * 1.5;
xCalTf.setText("" + newValue);
}
}
 
C

Chris Uppal

Philipp said:
I try to use a JFormattedTextField to show and enter numbers but can't
really figure out how to make it work.

Try the appended code, it's just your code with a few changes:
+ We set the format's idea of how many decimal digits to use
explicitly.
+ We don't use setText(), but setValue() which respects the
requested formatting.
+ We don't use Double.parseDouble(), but ask the formatter to do the
parsing for us.

(Double.parseDouble() would barf on:
1,234.567
which is how that number is printed in my locale. In others it would be:
1.234,567
which Double.parseDouble() can't handle either ;-)

-- chris

=====================
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TestFrame
extends JFrame implements PropertyChangeListener
{
JFormattedTextField deltaXTf;
JFormattedTextField xCalTf;
NumberFormat format = NumberFormat.getNumberInstance();
Double zero = Double.valueOf(0.0);

public static void main(String[] args)
{
TestFrame n = new TestFrame();
}

public TestFrame()
{
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(4);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
deltaXTf = new JFormattedTextField(format);
xCalTf= new JFormattedTextField(format);
deltaXTf.addPropertyChangeListener("value", this);
getContentPane().add(deltaXTf, BorderLayout.NORTH);
getContentPane().add(xCalTf, BorderLayout.SOUTH);
deltaXTf.setValue(zero);
xCalTf.setValue(zero);
pack();
setVisible(true);
}

public void propertyChange(PropertyChangeEvent evt)
{
Object src = evt.getSource();
Number deltaXTfVal = zero;
try
{
deltaXTfVal = format.parse(deltaXTf.getText());
}
catch (ParseException e)
{
System.err.println(e);
}
double newValue = deltaXTfVal.doubleValue() * 1.5;
xCalTf.setValue(Double.valueOf(newValue));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top