Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextfield  RSS feed

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we have two textfields in a frame. If we type something in those fields and press enter. How to get the values of those textfields? When I am using getText() method I am able to get only the second text value. I need the values of two textfields.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do firstField.getText() and secondField.getText() - perhaps show us your code so that we can help you better.
 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Rachel,
When I am using two getText() methods it is taking only the second value but not the first value. I am giving the code below.

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if(source instanceof JTextField) {
txtField1 = (JTextField)event.getSource();
answer=(String)MathPackage.matchAnsTxtField1.getText();
txtField2 = (JTextField)event.getSource();
answer = answer+(String)MathPackage.matchAnsTxtField2.getText();
}
}
Here answer is a string and txtField1 and txtField2 are the JTextField objects.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!