This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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


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
 
GeeCON Prague 2014
 
subject: JFormattedTextField