my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes JFormattedTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JFormattedTextField" Watch "JFormattedTextField" New topic
Author

JFormattedTextField

Kim Mirtens
Greenhorn

Joined: Dec 17, 2006
Posts: 21
I have a JFrame with several JFormattedTextFields to get number input from the user. I use the following code to create my JFormattedTextField.

JFormattedTextField numberinput = new JFormattedTextField(NumberFormat.getInstance());

My first question is fairly simple: how do I get a number from my JFormattedTextField? I tried to do the following but always got a castException (when I changed the float to double it sometimes works, but I would like to asign the value to a floating point):

float input = (Float) numberinput.getValue();

The second question I have is a bit more difficult. I use a JSpinner with a numbermodel, so it actually also uses a JFormattedTextField to display my number values. I want to adjust this JFormattedTextField to always show 3 fraction digits. I use the following code to create my JSpinner:

SpinnerNumberModel model = new SpinnerNumberModel(1.000,1.000-0.501,1.000+0.501,0.001);
JSpinner spinnumbers = new JSpinner(model);
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Tim Pintens:
how do I get a number from my JFormattedTextField? I tried to do the following but always got a castException (when I changed the float to double it sometimes works, but I would like to asign the value to a floating point):

float input = (Float) numberinput.getValue();


float input = ((Number)numberinput.getValue()).floatValue();


bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Tim Pintens:
I I use a JSpinner with a numbermodel, so it actually also uses a JFormattedTextField to display my number values. I want to adjust this JFormattedTextField to always show 3 fraction digits. I use the following code to create my JSpinner:

SpinnerNumberModel model = new SpinnerNumberModel(1.000,1.000-0.501,1.000+0.501,0.001);
JSpinner spinnumbers = new JSpinner(model);


spinnumbers.setEditor(new JSpinner.NumberEditor(spinnumbers, "0.000"));
Kim Mirtens
Greenhorn

Joined: Dec 17, 2006
Posts: 21
Thank you very much
 
wood burning stoves
 
subject: JFormattedTextField
 
Similar Threads
JSpinner & SpinnerNumberModel
JFormattedTextField problem
JTextField with arrow listener(up&down).
A Multitude of questions
JSpinner does not work for Numbers