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

JTextField Question

 
Jacob H
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two fields and a Jbutton.
I'd like to get the text from the fields (after button is pressed) and put them in an array list of objects.
myList.add(new Object(Field1, field2)); (This line already tested and works)
in my mouseReleased method I test to see if the button is pressed:
JButton b = (JButton)event.getSource();
if (b == Accept)
{String name = field1.getText();
String num = field2.getText();
myList.add(new Object(field1, field2));
}
I'm obviously not getting the right results.
Need some help with this, thanks.
 
Chris Brat
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
since you are trying to add the text of the two fields to the Object, the following might be your problem
-> myList.add(new Object(field1, field2));
with that line you arent adding the text to the object - you are adding the TextField. shouldn't it rather be...
myList.add(new Object(name,num));

This will be adding the text.
Just a few questions..
- Why are you calling your object 'Object'? wouldnt it be better to give it its own descriptive name?
- If you are storing pairs of values why dont you have a look at the Maps like Hashtable etc. Could save you alot of aggravation and coding.
Chris
 
kishore harindran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use equals method for checking :
JButton b = (JButton)event.getSource();
if (b.equals("Accept"))
{String name = field1.getText();
String num = field2.getText();
myList.add(new Object(field1, field2));
}
or :
Object obj = event.getSource();
if(obj.equals(jButtonName)){
// write ur own code.
}
where jButtonName - variable name defined for JButton.
 
Jacob H
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the user have to press enter in each field inorder to store it (getText())?
Originally posted by kishore harindran:
use equals method for checking :
JButton b = (JButton)event.getSource();
if (b.equals("Accept"))
{String name = field1.getText();
String num = field2.getText();
myList.add(new Object(field1, field2));
}
or :
Object obj = event.getSource();
if(obj.equals(jButtonName)){
// write ur own code.
}
where jButtonName - variable name defined for JButton.
 
Jacob H
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this whole thing figured out.
Thanks for all the help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic