Problem with Key listener to JComboBox
Hi,
I am trying to add a key Listener to a JComboBox so that when you hit the delete key, it deletes the item selected from the Combo box. Here is the code that registers the listener to the combo box.
Code :
jcboCountries.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("In key pressed");
if (e.getKeyCode() == KeyEvent.VK_DELETE){
System.out.println("Delete key hit");
Object item = model.getSelectedItem();
jcboCountries.removeItem(item);
}
}
});
I also added this line to set the combo box focus.
Code :
jcboCountries.setFocusable(true);
But when I hit the delete button nothing happens. The println lines don't print. Nothing.
Any help is appreciated.
Re: Problem with Key listener to JComboBox
Does the JComboBox have the focus? That determines what component gets key events.
Re: Problem with Key listener to JComboBox
Quote:
Originally Posted by
Norm
Does the JComboBox have the focus? That determines what component gets key events.
I thought so. I thought jcboCountries.setFocusable(true) was supposed to do that. Am I using the wrong method?
Re: Problem with Key listener to JComboBox
Add a focus listener to it and see if it gets the focus.
Also when using Adapters, be sure to use the @Override notation. The compiler is happy to let you add any methods you want. You want to be sure you are overriding one of the Adapter's methods and not adding a new one.
Re: Problem with Key listener to JComboBox
Quote:
Originally Posted by
Norm
Add a focus listener to it and see if it gets the focus.
Also when using Adapters, be sure to use the @Override notation. The compiler is happy to let you add any methods you want. You want to be sure you are overriding one of the Adapter's methods and not adding a new one.
I added the focus listener but nothing happened so it is not getting focus. Here is my entire code.
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise35_13 extends JApplet {
private final static int NUMBER_OF_NATIONS = 7;
private String[] nations = new String[] {"Denmark",
"Germany", "China", "India", "Norway", "UK", "US"};
private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS];
private ImageIcon[] bigIcons = new ImageIcon[NUMBER_OF_NATIONS];
// Create a combo box model
private DefaultComboBoxModel model = new DefaultComboBoxModel();
// Create a combo box with the specified model
private JComboBox jcboCountries = new JComboBox(model);
// Create a list cell renderer
private MyListCellRenderer renderer = new MyListCellRenderer();
// Create a label for displaying iamge
private JLabel jlblImage = new JLabel("", JLabel.CENTER);
/** Construct the applet */
public Exercise35_13() {
// Load small and large image icons
for (int i = 0; i < NUMBER_OF_NATIONS; i++) {
icons[i] = new ImageIcon(getClass().getResource(
"/image/flagIcon" + i + ".gif"));
model.addElement(new Object[]{icons[i], nations[i]});
bigIcons[i] = new ImageIcon(getClass().getResource(
"/image/flag" + i + ".gif"));
}
// Set list cell renderer for the combo box
jcboCountries.setFocusable(true);
jcboCountries.setRenderer(renderer);
jlblImage.setIcon(bigIcons[0]);
add(jcboCountries, java.awt.BorderLayout.NORTH);
add(jlblImage, java.awt.BorderLayout.CENTER);
// Register listener
jcboCountries.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jlblImage.setIcon(bigIcons[jcboCountries.getSelectedIndex()]);
}
});
// Register listener
jcboCountries.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("In key pressed");
if (e.getKeyCode() == KeyEvent.VK_DELETE){
System.out.println("Delete key hit");
Object item = model.getSelectedItem();
jcboCountries.removeItem(item);
}
}
});
// Register listener
jcboCountries.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
System.out.println("Focus Gained");
}
});
}
/** Main method */
public static void main(String[] args) {
ComboBoxCellRendererDemo applet = new ComboBoxCellRendererDemo();
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("ComboBoxCellRendererDemo");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Re: Problem with Key listener to JComboBox
the posted code does not compile without changes.
Using the code as an applet, it works for me. Running it in AppletViewer:
Focus Gained
key pressed e=java.awt.event.KeyEvent[KEY_PRESSED,keyCode=127,keyText=Delete,keyChar=Del ete,keyLocation=KEY_LOCATION_STANDARD] on javax.swing.JComboBox[,0,0,500x25,layout=javax.swing.plaf.metal.MetalCom boBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0, alignmentY=0.0,border=,flags=16777544,maximumSize= ,minimumSize=,preferredSize=,isEditable=false,ligh tWeightPopupEnabled=true,maximumRowCount=8,selecte dItemReminder=[Ljava.lang.Object;@b6548]
Delete key hit
key pressed e=java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=Enter,keyChar=Enter ,keyLocation=KEY_LOCATION_STANDARD] on javax.swing.JComboBox[,0,0,500x25,layout=javax.swing.plaf.metal.MetalCom boBoxUI$MetalComboBoxLayoutManager,alignmentX=0.0, alignmentY=0.0,border=,flags=16777544,maximumSize= ,minimumSize=,preferredSize=,isEditable=false,ligh tWeightPopupEnabled=true,maximumRowCount=8,selecte dItemReminder=[Ljava.lang.Object;@1358f03]
Re: Problem with Key listener to JComboBox
Wow, you are right. I put the class file in an html page and it worked fine as an applet. But it does not run as a standalone application.
Thanks