Skip to main content

JTextField.addActionListener()

4 replies [Last post]
hsantos
Offline
Joined: 2005-04-07

Hi
I have a JTextField in an JOptionPane. When user hits the return key in the JTextField, the JOptionPane closes.
But when I add an ActionListener to the JTextField, the JOptionPane no longer closes.
What's wrong ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hsantos
Offline
Joined: 2005-04-07

Thanks Shannon

Now I try to figure out how to have my ActionListener but also the default behaviour fired on return key pressed.
Do I have to add a second ActionListener that does the job? And what exactly should it do ?

Huguette

shan-man
Offline
Joined: 2006-02-17

Hi Huguette,

> Thanks Shannon
>
> Now I try to figure out how to have my ActionListener
> but also the default behaviour fired on return key
> pressed.
> Do I have to add a second ActionListener that does
> the job? And what exactly should it do ?
>
> Huguette

Depending on what you're using it for, you may not even need the ActionListener that you're adding. Could you explain some more about this JOptionPane? What does your ActionListener do? Can you post the code that creates this JOptionPane and ActionListener?

Thanks!
Shannon

hsantos
Offline
Joined: 2005-04-07

Shannon,

your questions helped me a lot!!
In my application, I subclassed JTextField to add some additional features and one of them is this ActionListener which in fact is not required all the time especially when the JTextField is put in a JOptionPane.
I only changed one line of code and now the ActionListener is added only when needed.

Thanks a lot
Huguette

PS: I added this ActionListener to fire a PropertyChangeEvent when user hits return. Is there another way to do this without using an ActionListener ?
[code]
public void addPropertyChangeListener( String propertyName, PropertyChangeListener listener) {
super.addPropertyChangeListener( propertyName, listener);
if( PROPERTY_FILTER.equals( propertyName)) {
addActionListener( this);
}
}

public void actionPerformed( ActionEvent e) {
if( e.getSource().equals( this)) {
firePropertyChange( PROPERTY_FILTER, null, getText());
}
}

[/code]

shan-man
Offline
Joined: 2006-02-17

Hi hsantos,

> Hi
> I have a JTextField in an JOptionPane. When user hits
> the return key in the JTextField, the JOptionPane
> closes.
> But when I add an ActionListener to the JTextField,
> the JOptionPane no longer closes.
> What's wrong ?

The return/enter key in Swing is wired up to activate the GUI's default button, if there is one (which there probably is in your JOptionPane), unless it is consumed by something else. Hitting it while the focus is on any component in the dialog is the same as clicking on the default button. In your JOptionPane, the default button behavior causes the dialog to close.

Adding an ActionListener to the JTextField changes the behavior of the enter key when the focus is on that field. It will now cause your ActionListener to be notified when it is pressed.

Hope that helps!
Shannon