Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField

 
Kim Mirtens
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic