JTextField and semantic events

If I understand well I should use addActionListener to check for semantic events on a JTextField. This action event gets fired when hitting the enter key. But I want to listen to changes to the value of the text field. In fact when the user pauses typing for a while (say a second), or when the field looses focus.

What would be an elegant (and simple) solution?

Jo

[384 byte] By [Jo-fa] at [2007-10-2 9:11:49]
# 1

Well, you are going to have to use multiple listeners and varied bits of code to do this, so I do not think there is a "simple" solution.

An elegant solution would be to gather all of this code into one class that knows how to register itself wherever necessary (maybe upon construction). So instead of:

MySpiffyMultiListenerClass listener = new MySpiffyMultiListenerClass();

JTextField fooField = new JTextField();

fooField.getDocument().addDocumentListener(listener);

fooField.addFocusListener(listener);

// and so on...

you just instantiate the listener passing the JTextField as an argument, and it registers itself where necessary:

JTextField fooField = new JTextField();

MySpiffyMultiListenerClass listener = new MySpiffyMultiListenerClass(fooField);

Drake

Drake_Duna at 2007-7-16 23:18:52 > top of Java-index,Java Essentials,New To Java...
# 2

For a while I was puzzled what you meant, but then discovered I already wrote the MultiListener class with a constructor that performed the addActionListioner and other types of events I tried.

So thanks for confirming the elegance of my intuitive solution.

As the content should be restricted to numbers (in this particular case even at most a single digit, in another application more complex numbers), I'm focussing now on JFormattedTextField. The InputVerifier looks useful at first sight, but what could cover the user "doesn't type anymore for a while"?

Jo

Jo-fa at 2007-7-16 23:18:52 > top of Java-index,Java Essentials,New To Java...
# 3

> focussing now on JFormattedTextField. The

> InputVerifier looks useful at first sight, but what

> could cover the user "doesn't type anymore for a

You are going to have to write your own code for that. You already know how to detect user input. How about if you start a timer on a separate thread when you render the page visible (or when input starts), reset it every time there is new input, and then have it invokeLater() back onto the event thread if it times out before there is new input?

Drake

Drake_Duna at 2007-7-16 23:18:53 > top of Java-index,Java Essentials,New To Java...
# 4

> How about if you start a timer on a separate thread

> ...

A lot more to learn. I'll leave that for later.

As for the InputVerifier, the documentation says ... Before focus is transfered to another Swing component...

But it reacts only on the enter-key. There are so much more possibillities to loose focus: the mouse, the tab-key, arrow keys, an alt-key cobmibation (hot-key / accelerator) for another field. Perhaps I'm spoiled with Delphi which fires the same event in all these occasions.

Jo

Jo-fa at 2007-7-16 23:18:53 > top of Java-index,Java Essentials,New To Java...
# 5

I am not sure what you are trying to do here. InputVerifier is for locking the focus onto a component until a valid string of characters has been entered into it. I ran the little demo that shows up at the top of the API, and was unable to transfer focus by any means until I entered the correct text.

If you are trying to use the InputVerifier to get your "user doesn't type for a while" functionality then you are barking up the wrong tree.

Drake

Drake_Duna at 2007-7-16 23:18:53 > top of Java-index,Java Essentials,New To Java...
# 6

Here's some code I wrote that uses a javax.swing.Timer for something a lot like that:private void startHoverTimer() {

hovering = new Timer(500, new ActionListener() {

public void actionPerformed(ActionEvent e) {

hoverOver();

}

});

hovering.start();

}

private void stopHoverTimer() {

if (hovering != null) {

hovering.stop();

hovering = null;

}

}

So when a key is pressed, or whatever it is, call startHoverTimer(). If nothing else happens within 500 milliseconds (or whatever you change that to), then your hoverOver() method is called. If something else happens, like a key is pressed, then call stopHoverTimer() to kill the timer.

DrClapa at 2007-7-16 23:18:53 > top of Java-index,Java Essentials,New To Java...