How can I access text entered in a JComboBox, when the user does not hit enter?

Sandip Chitale

The easiest way to do this is to watch for changes in focus. WHen the user leaves the combo box (to press a button or work with another component, for example) you can catch a focusLost event from the JComboBox's editor.

The following code assumes you haven't changed the editor for the combo box (which is probably the case).

if (comboBox.getEditor().getEditorComponent()
    instanceof JTextField) {
  JTextField tf = 

  tf.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent fe) {
      // get the text using getText() method
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact