File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextField Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField Question" Watch "JTextField Question" New topic
Author

JTextField Question

Jacob H
Greenhorn

Joined: Oct 03, 2003
Posts: 9
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

Joined: May 22, 2003
Posts: 108
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


SCJP 1.2, SCJP 5, SCBCD
kishore harindran
Greenhorn

Joined: Dec 04, 2002
Posts: 14
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

Joined: Oct 03, 2003
Posts: 9
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

Joined: Oct 03, 2003
Posts: 9
Got this whole thing figured out.
Thanks for all the help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField Question
 
Similar Threads
Threads and ActionListeners
JOptionPane with Multiple InputText boxes
Getting JPanel size
JOptionPane.showInputDialog to accept multiple inputs
Custom Dialog - please help