Skip to main content

JComboBox ItemListener passing typed key to JTextField

No replies
johnsmith2010
Offline
Joined: 2010-05-06
Points: 0

I am having a problem trying to change a GUI when the user selects a certain JComboBox list item. Everything works great if user clicks the item with a mouse, or with the arrow keys. However, when using the key accelerators, the issue arises.

Basically, I have a JComboBox with an "Other" list item. If the user selects this item, I wish to hide the combo box and display a text field. The issue is that if the user clicks the "O" key on the keyboard to select "Other", then an "O" is transferred/copied/whatever to the text field. I have tried setting the text field to null in the item listener for the JComboBox and also in the focusGained of the text field. Nothing works.

Has anyone ever had the need to do something like this? How did you solve it? I am open to all suggestions. It's driving me crazy :)

Here is some example code. If you use the mouse to select "Other" or use the arrow keys, everything is fine. If you type "O", you will see an "O" in the text field.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxExample
extends javax.swing.JPanel
implements java.awt.event.ActionListener
{
public ComboBoxExample() {
super( new BorderLayout() );

String[] petStrings = { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" };

final JTextField petDescription = new JTextField();
final JComboBox petList = new JComboBox( petStrings );
petList.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent itemEvent ) {
if ( !itemEvent.getSource().equals( petList ) ) {
return;
}
if ( itemEvent.getStateChange() != ItemEvent.SELECTED ) {
return;
}

if ( petList.getSelectedIndex() == 0 ) {
petDescription.setText( null );
return;
}

//
// If the user has chosen "Other", then show the text field.
//
if ( petList.getSelectedIndex() == ( petList.getItemCount() - 1 ) ) {
petDescription.setText( null );
petDescription.requestFocus();
petDescription.dispatchEvent(
new FocusEvent(
petDescription,
FocusEvent.FOCUS_GAINED,
false
)
);
petList.setVisible( false );
petDescription.setVisible( true );
}
}
}
);
petList.setSelectedIndex( 0 );
petList.addActionListener( this );
petList.setVisible( true );
petDescription.setVisible( false );

add( petList, BorderLayout.PAGE_START );
add( petDescription, BorderLayout.PAGE_END );
this.setBorder( BorderFactory.createEmptyBorder( 20, 20, 20, 20 ) );
}

public void actionPerformed(ActionEvent e) {
JComboBox cb = ( JComboBox ) e.getSource();
System.out.println( ( String ) cb.getSelectedItem() );
}

private static void createAndShowGUI() {
JFrame frame = new JFrame( "ComboBoxExample" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JComponent newContentPane = new ComboBoxExample();
newContentPane.setOpaque( true );
frame.setContentPane( newContentPane );
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}