how to make Java notice ActionEvent after changing focus?

M

Mark_Galeck

Hello, typically with other GUIs (for example MS Windows), if you
edit a text field, you don't have to press Enter for the system to
notice - for example, you can click your mouse on another field, and
the system notices the change.

It appears this is not so with Java Swing - I edit the text value in
JTextField, and I have to press Enter for ActionEvent to show up, if I
click on another field, the changed value stays in the text field, but
no ActionEvent is fired. This makes no sense to me !?

How to make ActionEvent fire if I start editing something else??

Thank you for your insight,

Mark
 
N

Nigel Wade

Mark_Galeck said:
Hello, typically with other GUIs (for example MS Windows), if you
edit a text field, you don't have to press Enter for the system to
notice - for example, you can click your mouse on another field, and
the system notices the change.

So does Swing, but you have to register your interest in that change so that you
get notified which doesn't happen by default.
It appears this is not so with Java Swing - I edit the text value in
JTextField, and I have to press Enter for ActionEvent to show up, if I
click on another field, the changed value stays in the text field, but
no ActionEvent is fired. This makes no sense to me !?

Swing is more flexible. ActionEvent is fired when an action occurs, and losing
focus is not an action. If you want to know when the Component loses focus you
register a FocusListener.
How to make ActionEvent fire if I start editing something else??

You can't [directly], but you will receive a FocusEvent if the focus is moved to
another Component and you have registered a FocusListener.
 
M

Mark_Galeck

OK, from the documentation, it seems like InputMethodListener should
be what I need, it says an event will be sent if the text changes -
but, I don't get those events no matter whether I lose focus out of
JTextField or press Enter, here's the code:

class Foo extends JPanel implements InputMethodListener {
(...)
txt = new JTextField();
(...)
txt.addInputMethodListener(this);

public void inputMethodTextChanged(InputMethodEvent e) {
(...)
}

}


and inputMethodTextChanged, never gets called, no matter what I do
with the contents of the text field
 
M

Mark_Galeck

OK, I understand, of course I did not implement the interface
InputMethodRequests, yet, this seems way too much for a text field.

So, please experienced Java programmers, tell me, how do you notice
that a text in JTextField has changed - like, the user edited the
field, and either pressed Enter or clicked elsewhere

It looks to me like there is no single event handler, one has to
listen for both Action and Focus - is this right? Seems strange, as
both events really do the same thing - commit the text in a
JTextField, so there ought to be one event, no? Sorry for being a
newbie...

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top