user3176405
user3176405

Reputation: 11

Listener on JTextField to detect completion of data entry

We are developing an application in Java Swing.

We have a text field and a combo box. The combo box values should be populated from DB based on the value entered in the text field.

The text field length is 4 characters. So, user can enter any value between 1 to 9999.

Which listener would be used to identify that the user has completed their entry in the text field so that I can populate the combo box?

Upvotes: 1

Views: 97

Answers (2)

nyulan
nyulan

Reputation: 321

Use the textField.getDocument().addDocumentListener(new DocumentListener()

  textField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    //TODO
  }
  public void removeUpdate(DocumentEvent e) {
   //TODO
  }
  public void insertUpdate(DocumentEvent e) {
  //TODO
 }

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

..the user has completed his entry in the text field so that i can populate the combobox.

An ActionListener. When the user is done, they hit the enter key and an action event will be fired. But..

..user can enter any value between 1 to 9999.

This sounds better suited to a JSpinner using a SpinnerNumberModel and a ChangeListener to register selecting a different number.

Upvotes: 3

Related Questions