我如何获得上一个或最后一个项目?

时间:2022-03-02 14:30:08

How do I get the last or previous or unselected item and then the new item for a JComboBox?

如何获取上一个或上一个或未选择的项目,然后获取JComboBox的新项目?

1 个解决方案

#1


10  

I assume this applies to all Objects that allow for which you to add Item Listeners to them.

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

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());
   }
}

#1


10  

I assume this applies to all Objects that allow for which you to add Item Listeners to them.

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

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());
   }
}