4

如何获取最后一个或上一个或未选中的项目,然后是 a 的新项目JComboBox

4

1 回答 1

12

我假设这适用于所有允许您向它们添加项目侦听器的对象。

String[] items = {"item 1","item 2"," item 3","item 4"};
JComboBox combo = new JComboBox(items)
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{
   if(ie.getStateChange() == ItemEvent.DESELECTED) //edit: bracket was missing
   {
      System.out.println("Previous item: " + ie.getItem()); //edit: bracket was missing
   }
   else if(ie.getStateChange() == ItemEvent.SELECTED)
   {
      System.out.println("Current \ New item: " + ie.getItem());
   }
}
于 2013-02-01T13:32:56.863 回答