I want the message box to appear immediately after the user changes the value in the textfield. Currently, I need to hit the enter key to get the message box to pop out. Is
I know this relates to a really old problem, however, it caused me some problems too. As kleopatra responded in a comment above, I solved the problem with a JFormattedTextField
. However, the solution requires a bit more work, but is neater.
The JFormattedTextField
doesn't by default trigger a property change after every text changes in the field. The default constructor of JFormattedTextField
does not create a formatter.
However, to do what the OP suggested, you need to use a formatter which will invoke the commitEdit()
method after each valid edit of the field. The commitEdit()
method is what triggers the property change from what I can see and without the formatter, this is triggered by default on a focus change or when the enter key is pressed.
See http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#value for more details.
Create a default formatter (DefaultFormatter
) object to be passed to the JFormattedTextField
either via its constructor or a setter method. One method of the default formatter is setCommitsOnValidEdit(boolean commit)
, which sets the formatter to trigger the commitEdit()
method every time the text is changed. This can then be picked up using a PropertyChangeListener
and the propertyChange()
method.