ItemListener for ComboBox

I have this scenario: I want to pick up a particular item from a combo box using my up and down keys. After selecting the item, if I press enter, it should pop up a dialog.

The combo box listener should also listen to mouse events.

Can using itemStateChanged(ItemEvent e) of ItemListener for the combo box make this happen?

I saw that it has got a method getStateChanged(ItemEvent e) in it.

I tried this:

public void itemStateChanged(ItemEvent e)

{ if ((e.getStateChange() == ItemEvent.SELECTED)

..........

}

This however, detects only the up and down arrow movements, but doesn't meet the requirements that I have.

Is there any other listeners for ComboBox that would be best suited for this situation?

Thanks

cooler

[798 byte] By [coolera] at [2007-10-1 22:23:11]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class EnterKey

{

JDialog dialog;

public EnterKey()

{

initDialog();

}

private void initDialog()

{

dialog = new JDialog(new Frame(), "dialog", false);

dialog.getContentPane().add(new JLabel("hello world", JLabel.CENTER));

dialog.setSize(100,100);

dialog.setLocation(425, 200);

}

private JPanel getComboPanel()

{

String[] birds = {

"crow", "bluebird", "hawk", "crane",

"ibis", "heron", "bluejay", "robin"

};

JComboBox combo = new JComboBox(birds);

registerKey(combo);

combo.addItemListener(new ComboListener());

JPanel panel = new JPanel();

panel.add(combo);

return panel;

}

private void registerKey(JComponent c)

{

Object name = showDialog.getValue(Action.NAME);

c.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), name);

c.getActionMap().put(name, showDialog);

}

private Action showDialog = new AbstractAction("showDialogAction")

{

public void actionPerformed(ActionEvent e)

{

dialog.setVisible(true);

}

};

private class ComboListener implements ItemListener

{

public void itemStateChanged(ItemEvent e)

{

if(e.getStateChange() == ItemEvent.SELECTED)

{

System.out.println(e.getItem());

}

}

}

public static void main(String[] args)

{

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new EnterKey().getComboPanel());

f.setSize(200,100);

f.setLocation(200,200);

f.setVisible(true);

}

}

74philipa at 2007-7-13 8:37:15 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...