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);
}
}
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);
}
}