如何获得上一个或最后一个项目? [英] How do I get the previous or last item?

查看:37
本文介绍了如何获得上一个或最后一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我假定这适用于所有允许向其添加项目侦听器的对象.

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

这篇关于如何获得上一个或最后一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆