How do I get the previous or last item?

天涯浪子 提交于 2019-12-12 12:15:15

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/14647439/how-do-i-get-the-previous-or-last-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!