// JP opened flex table

Click to See Complete Forum and Search --> : JTextFields


brw2008
July 21st, 2008, 06:17 PM
Hi all,

Thank you for those who helped me on my last topic! It is taking me a while to get back into Java, and i cannot remmeber how to return an integer than has been inputted into a JTextField.

I need to get the number that the user will enter, and just simply print it for now....

The code to help i know is simple and easy i just cnnot find it on internet and if anyone on here can help it would be brilliant!

Thanks.

dlorde
July 21st, 2008, 06:23 PM
If you want to print the contents of a JTextField, just use: System.out.println(textField.getText());A prudent question is one-half of wisdom...
F. Bacon

compavalanche
July 21st, 2008, 06:23 PM
Is this what your looking for?

JTextField j = new JTextField();
int mynum = Integer.parseInt(j.getText());

As for docs I usually like to look at the API docs

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTextField.html

Or just open up an editor and use intellisense/code completion to help you out.

Hope that helps...

brw2008
July 21st, 2008, 06:36 PM
ahhh thank you for that second response, the first response is what i was doing already, but i needed to parse it as an Integer! That is what i forgot thank you so much for the reply! Also, guys is there any way to format the text field so only numbers can be entered??

Many thanks

compavalanche
July 21st, 2008, 06:44 PM
Take a look at this:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFormattedTextField.html

This will allow you to write a listener and you can accept or reject the inputs.

brw2008
July 21st, 2008, 06:53 PM
thank you very much i shall have a look! much appreciated

brw2008
July 21st, 2008, 07:10 PM
Well...i have looked at that, and it doesn't make much sense to me...i dunno if it's just me though?? I mean i understand that there is a subclass of JTextField whereby I can use JFormattedTextField, but how do i code it so i can implement it as a number so no written letters can be input??

If you can help with this it would be most appreciated,

Cheers

dlorde
July 21st, 2008, 07:33 PM
i needed to parse it as an Integer!But instead, you said: "I need to get the number that the user will enter, and just simply print it for now....".

Also, guys is there any way to format the text field so only numbers can be entered??To restrict input to numeric characters use a DocumentFilter (http://java.sun.com/javase/6/docs/api/javax/swing/text/DocumentFilter.html). See Implementing A DocumentFilter (http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter).

The hardest part of the software task is arriving at a complete and consistent specification, and much of the essence of building a program is in fact the debugging of the specification...
F. Brooks

cout42
July 25th, 2008, 06:42 AM
Also, guys is there any way to format the text field so only numbers can be entered??

Here is a simple program to do just that.
http://faq.javaranch.com/java/NumericTextField

dlorde
July 25th, 2008, 08:21 AM
Here is a simple program to do just that.
http://faq.javaranch.com/java/NumericTextFieldThat will certainly work, but rather than subclassing the document, the recommended way is to set a DocumentFilter on it - that's what DocumentFilters are for. The code will be very similar, but the DocumentFilter approach is more flexible and can be used with any AbstractDocument.

Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho & J. Ullman

//JP added flex table