Forums Register Login

JFormattedTextField

+Pie Number of slices to send: Send
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);
+Pie Number of slices to send: Send
 

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();
+Pie Number of slices to send: Send
 

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"));
+Pie Number of slices to send: Send
Thank you very much
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1546 times.
Similar Threads
JSpinner does not work for Numbers
A Multitude of questions
JSpinner & SpinnerNumberModel
JTextField with arrow listener(up&down).
JFormattedTextField problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:08:19.