Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the previous or last item?

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

like image 791
Craig Wayne Avatar asked Dec 20 '22 10:12

Craig Wayne


1 Answers

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());
   }
}
like image 75
Craig Wayne Avatar answered Dec 30 '22 12:12

Craig Wayne