aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Handling multiple textfields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Handling multiple textfields" Watch "Handling multiple textfields" New topic
Author

Handling multiple textfields

Tasdiq Ameem
Greenhorn

Joined: May 02, 2012
Posts: 5

Hi

I have three textfields and a button in the window. I want the program to store whatever user types in each textfields , in three separate strings , WHEN the user clicks the button. Can anyone please give teh code for just the handler class or the actionPerformed() method. or if you don't wanna type in the code, please explain the syntax that would allow me to do it.

I'm stuck with this problem and really need help as soon as possible. Thank you, I appreciate it.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I want the program to store whatever user types in each textfields , in three separate strings , WHEN the user clicks the button.

1) store them where?

2) how would you do ii if there was only one textField?
Tasdiq Ameem
Greenhorn

Joined: May 02, 2012
Posts: 5

Michael Dunn wrote:> I want the program to store whatever user types in each textfields , in three separate strings , WHEN the user clicks the button.

1) store them where?

2) how would you do ii if there was only one textField?


1) I want to store them in three separate String variables.

2) Here is what I do when I have only one textField , the user types in that and hits enter:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 2) Here is what I do when I have only one textField...

have you tested that by printing out the value of 'str'?
Tasdiq Ameem
Greenhorn

Joined: May 02, 2012
Posts: 5

Michael Dunn wrote:> 2) Here is what I do when I have only one textField...

have you tested that by printing out the value of 'str'?


not in console, but yes I was able to and I did print it out in GUI.

I'm assuming getActionCommand() gets the input (when user hits enter). My question is, how do I do that simultaneously for more than one textfields
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
post a working example of that code
displaying (sys.out/optionPane/other)
the user's input to the textfield, and I'll
add two textfields and the necessary
adjustments to your handler code, so
it'll do what you want.

your code only needs to be small:
frame/textfield/button/handler
just mske sure it compiles/runs/outputs ok
Tasdiq Ameem
Greenhorn

Joined: May 02, 2012
Posts: 5

Michael Dunn wrote:post a working example



I can execute above code with a class with main method like this, runs without errors:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> ... and a button in the window...

where's the button in your example?

basically, you need textField's getText() in your handler,
which will produce
str1 = tf1.getText();
str2 = tf2.getText();
str3 = tf3.getText();
then it's up to you how you want to store the str's

you could even do this
str = tf1.getText()+" "+tf2.getText()+" "+tf3.getText();

if saved as one line in a textFile, to retrieve you'd read the line,
then split() on the delimeter (the delimeter can be anything, I've used a 'space')
Tasdiq Ameem
Greenhorn

Joined: May 02, 2012
Posts: 5

That is exactly what I have been looking for. Thank you.
The thing is I just started learning GUI stuff in Java but was not familiar with all the syntax necessary for my assignment.

Another thing....
Is there a boolean expression to check whether or not a textField is empty?
Where is the getText() method inherited from?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Is there a boolean expression to check whether or not a textField is empty?

if empty, getText() will return an empty string "", so the boolean test is .equals("")

> Where is the getText() method inherited from?

go to the api docs for JTextField - the inherited methods are listed there (scroll down a bit)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling multiple textfields