AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TextField to accept only Integer values.

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
vijju
Guest





PostPosted: Thu May 04, 2006 5:07 am    Post subject: TextField to accept only Integer values. Reply with quote



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





PostPosted: Thu May 04, 2006 7:07 am    Post subject: Re: TextField to accept only Integer values. Reply with quote



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





PostPosted: Thu May 04, 2006 7:07 am    Post subject: Re: TextField to accept only Integer values. Reply with quote



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.
}


Quote:
Regards,
Vijay

DM
Back to top
Thomas Hawtin
Guest





PostPosted: Thu May 04, 2006 1:07 pm    Post subject: Re: TextField to accept only Integer values. Reply with quote

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





PostPosted: Mon May 08, 2006 11:07 pm    Post subject: Re: TextField to accept only Integer values. Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.