Enable a search button as soon as a character is entered in text field

I have a text field (java swing) and a search button.

When the user hits RETURN, the text field fires an action event.

Is it possible that as soon as I enter a character in the text field, it fires an action event without hitting the RETURN key or pressing the search button?

What I want to achieve is, if the user does not enter any text in the text field, then the search button is disable. As soon as a character is entered in the text field the search button is enable. How can I do this?

// Create a text field with some initial text

JTextField textfield =new JTextField("Initial Text");

// Create a text field with some initial text and a default number of columns.

// The number of columns controls the preferred width of the component;

// each column is rougly the size of an M in the current font.

int cols = 30;

textfield =new JTextField("Initial Text", cols);

// Listen for action events, which are fired when the user hits RETURN

textfield.addActionListener(new MyActionListener());

class MyActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent evt){

JTextField textfield = (JTextField)evt.getSource();

process(textfield.getText());

}

}

[1868 byte] By [SDNJavaa] at [2007-11-27 11:51:23]
# 1

Use a CaretListener (javax.swing.event.*)

ICE

icewalker2ga at 2007-7-29 18:37:50 > top of Java-index,Desktop,Core GUI APIs...
# 2

Use a DocumentListener.

Don't forget to disable the button when text is removed as well.

camickra at 2007-7-29 18:37:50 > top of Java-index,Desktop,Core GUI APIs...