Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Handling multiple textfields

 
Tasdiq Ameem
Greenhorn
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> ... 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
Posts: 5
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic