• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextField Question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got this whole thing figured out.
Thanks for all the help!
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic