aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Clear a JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Clear a JTextField" Watch "Clear a JTextField" New topic
Author

Clear a JTextField

Judy Zhu
Greenhorn

Joined: Sep 24, 2001
Posts: 4
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.
Jeff Chang
Greenhorn

Joined: Aug 23, 2001
Posts: 23
Judy,
Please post your code here so that somebody maybe help you solve the problem. :-)
Judy Zhu
Greenhorn

Joined: Sep 24, 2001
Posts: 4
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

Joined: Aug 23, 2001
Posts: 23
Why don't you pass ActionEvent when you call xxxButton.addActionListener(..)?
Judy Zhu
Greenhorn

Joined: Sep 24, 2001
Posts: 4
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.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
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

Joined: Sep 24, 2001
Posts: 4
Great, it works! Thanks, Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clear a JTextField
 
Similar Threads
problem using validator framework with DispatchAction
Getting html form name
Cancel button and bypassing JavaScript validation
form design
cancel button issue