• Post Reply Bookmark Topic Watch Topic
  • New Topic

Duplicates from JTextField  RSS feed

 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have been pulling my hair out with this problem. Every time i use a JTextField instead of adding one thing, it adds the string typed into to the object set in action listener but then it adds an empty object. This has had me baffled for over a week. I have tested the Add methods in the calling class and they work fine, it only happens when i try and pass text using an action listener.
I am using JDK 1.5 i dont know if it something different in there but can someone please put me out of my misery.
This is the action listener that extracts the text, also the method it calls - thanks guys


<Code>
class AddUserActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{

String nameIn = newUserField.getText();
if(nameIn != null)
{
fc.addUser(nameIn);
newUserField.setText("");

JOptionPane.showConfirmDialog(null, nameIn + " has been Added", "Confirmation", 2, 2);
setVisible(true);
}


}
}


public boolean addUser(String nameIn)
{
boolean userAdded = false;
Favourites tempFav = new Favourites();


if(nameIn != null & favIn != null)
{
tempFav.setName(nameIn);
favIn.add(tempFav);
userAdded = true;
System.out.println("User Added :- " + tempFav.getName());
System.out.println("All Users:-" + getUsersNames().toString());


}
/*else
{
favIn = new ArrayList<Favourites>();
usersFavourites.setName(nameIn);
favIn.add(usersFavourites);
userAdded = true;
System.out.println("User Added :- " + usersFavourites.getName());
System.out.println("All Users:-" + getUsersNames().toString());
//writeFavourites();

}*/
System.out.println("The current favourites are " + favIn.toString());
writeFavourites();
return userAdded;
}

</Code>
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you added the action listener twice to the text field? The second instance would get the empty string that you put into the text field the first time!

Ed
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!