 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
vijju Guest
|
Posted: Thu May 04, 2006 5:07 am Post subject: TextField to accept only Integer values. |
|
|
Hi,
In My application,I am having a textField ,which has to take Long Value
from the Runtime class.
But TextField.setText() takes only string.If i convert the Long to
String,The Long equivalent of the String will be Returned,This does not
FullFill my Purpose.
How can i take Integer values in the textField.
Regards,
Vijay |
|
| Back to top |
|
 |
Bart Cremers Guest
|
Posted: Thu May 04, 2006 7:07 am Post subject: Re: TextField to accept only Integer values. |
|
|
Use a JFormattedTextField instead:
JFormattedTextField field = new
JFormattedTextField(NumberFormat.getIntegerInstance());
field.setValue(new Integer(123));
Integer intValue = (Integer) field.getValue();
Bart |
|
| Back to top |
|
 |
Denis Guest
|
Posted: Thu May 04, 2006 7:07 am Post subject: Re: TextField to accept only Integer values. |
|
|
vijju:
| Quote: | In My application,I am having a textField ,which has to take Long Value
from the Runtime class.
But TextField.setText() takes only string.If i convert the Long to
String,The Long equivalent of the String will be Returned,This does not
FullFill my Purpose.
How can i take Integer values in the textField.
|
When the dialog (or the frame) is being closed insert:
try {
num = Integer.valueOf(textField.getText());
} catch (NumberFormatException nfe) {
//Here the code to execute if the text was not a number.
}
DM |
|
| Back to top |
|
 |
Thomas Hawtin Guest
|
Posted: Thu May 04, 2006 1:07 pm Post subject: Re: TextField to accept only Integer values. |
|
|
vijju wrote:
| Quote: |
In My application,I am having a textField ,which has to take Long Value
from the Runtime class.
But TextField.setText() takes only string.If i convert the Long to
String,The Long equivalent of the String will be Returned,This does not
FullFill my Purpose.
How can i take Integer values in the textField.
|
Assuming you are using Swing (and at least Java 1.4), adding a
DocumentFilter to the field's document is the way to go. There's an
example within the linked example:
http://www.jroller.com/resources/t/tackline/PayCalculator.java
You can see that example in action, with Java WebStart:
http://www.jroller.com/resources/t/tackline/PayCalculator.jnlp
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/ |
|
| Back to top |
|
 |
Developer Dude Guest
|
Posted: Mon May 08, 2006 11:07 pm Post subject: Re: TextField to accept only Integer values. |
|
|
I agree with Tom's suggestion; I wrote a general purpose DocumentFilter
which applies regular expressions. I can also limited the length of
what is entered and beep when the user tries to enter invalid input.
Figuring out the right RegEx for a given situation and debugging it is
sometimes hard (RegEx's found on the internet don't always work
correctly in the filter so it sometimes takes a little playing around)
- but once it works, it works well. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|