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());

}

}

[1869 byte] By [SDNJavaa] at [2007-11-27 11:47:59]
# 1

Try posting Swing questions in the Swing forum. That's where the experts are.

BigDaddyLoveHandlesa at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...
# 2

Try an InputMethodListener.

http://java.sun.com/j2se/1.3/docs/api/java/awt/event/InputMethodListener.html#caretPositionChanged(java.awt.event.InputMethodEvent)

drawimagea at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...
# 3

For what it's worth, I would use a DocumentListener. You want your button to be in synch with the *contents* of the text field, not the *gestures* of the user.

BigDaddyLoveHandlesa at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...
# 4

Could you please give a sample code of how i can do this

SDNJavaa at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...
# 5

> Could you please give a sample code of how i can do this

No. The example in the API documentation for DocumentListener should get you started. In fact you should already have looked at that documentation and hence should not need to ask this question.

DrClapa at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...
# 6

> Could you please give a sample code of how i can do this

You've already been asked in the past to post Swing related questions in the Swing forum. (you haven't)

In that same posting you where given a link to the Swing tutorial with the hope that you would actually take the time to read it before asking your next question. The tutorial actually has an example of how to write a DocumentListener.

I'll let you practice your searching and reading skills to find your old posting and read the tutorial. Writing a listener is a fundamental concept to using Swing. Once you learn how to write one, you should be able to write any listener because the concept is the same only the method names change.

camickra at 2007-7-29 18:16:23 > top of Java-index,Java Essentials,Java Programming...