Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Clear a JTextField

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form written by Swing. The form includes a JTextField, a JButton(submit) and another JButton(cancel). I hope that whenever the cancel button was clicked, the contents in the JTextField will be cleared. I added an action listener to the cancel button. In the actionPerformed function, I put the JTextField setText(""); But It did not working. when I clicked the cancel button, nothing changed in the JTextField. Do I need to put some addtional refresh function to make it work? Please help. Thanks.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judy,
Please post your code here so that somebody maybe help you solve the problem. :-)
 
Judy Zhu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is as following:
public class getResDetail extends JPanel {

public getResDetail() throws IOException,DatabaseException {

this.setLayout(new GridLayout(2, 1));
JPanel resFieldPane = new JPanel();
JLabel resLabel = new JLabel("Reservation Number : ");
final JTextField resField = new JTextField(15);
resFieldPane.add(resLabel);
resFieldPane.add(resField);
this.add(resFieldPane);

class getRecord implements ActionListener {

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
try {
if(cmd.equals("SUBMIT")) {
submitRequest();
}
else if(cmd.equals("CANCEL")) {
cancelReservation();
}
}
catch(IOException ioe) {
System.out.println("IOException");
}
catch(DatabaseException dbe) {
System.out.println(dbe.toString());
}
}

public void submitRequest(){
//some code here!!
}

public void cancelReservation()
{
resField.setText("");
}
}

JPanel submitPane = new JPanel();
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
submitButton.setActionCommand("SUBMIT");
cancelButton.setActionCommand("CANCEL");
submitButton.addActionListener(new getRecord());
cancelButton.addActionListener(new getRecord());
submitPane.add(submitButton);
submitPane.add(cancelButton);
this.add(submitPane);

.....
}
 
Jeff Chang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you pass ActionEvent when you call xxxButton.addActionListener(..)?
 
Judy Zhu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, I passed the new getRecord() to xxxButton.addActionListener(..) method, where getRecord is a subclass of ActionListener. Also, I used the getText() method in the submitRequest() function, it works fine, so I think it is not the problem of the action event.
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try moving JTextField reference outside the constructor. It also seems odd to create an inner class entirely within the constructor but....
I got it to work by moving the reference and other changes for my preference.
 
Judy Zhu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, it works! Thanks, Paul.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic