• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1083
  • Last Modified:

ItemListener and stateChange

Hi,


I have a JComboBox with an ItemListener...and I would like to know how can I know what is chosen before this event is happened?


Here is what I have...

JComboBox tmpBox = new JComboBox();
tmpBox.addItemListener(new tmpBoxListener());

class tmpBoxListener implements ItemListener
{
  public void itemStateChange(ItemEvent ie)
  {
     if (e.getStateChange() == ItemEvent.DESELECTED) {
       // here I wanna store the index of JComboBox which is just deselected
      //  the previous data  <-- how can I do so???
    } else {
     ...
    }
  }
}



Xenia
0
xenia27
Asked:
xenia27
  • 6
  • 5
  • 4
  • +1
3 Solutions
 
objectsCommented:
try:

e.getItem()
0
 
TimYatesCommented:
You know the object that lost selection:

Object o = e.getItem();

You know the JComboBox that fired this event:

JComboBox box = (JComboBox)e.getItemSelectable() ;

so just loop through the items in that combobox till it == o

that is your index...
0
 
objectsCommented:
if you're using DefaultComboBox model (or subclass) yuou can get the index using:

int index = model.indexOf(e.getItem());
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
that should be getIndexOf()

must be beer o'clock :)
0
 
TimYatesCommented:
> must be beer o'clock :)

Mmmmm....beer o'clock ;-)
0
 
xenia27Author Commented:
OK...I'm confused...
if ( ie.getStateChange() == ItemEvent.DESELECTED )  <--- what will this do?
{
  TmpCBox = (JComboBox)ie.getSource();
  LastSelect = TmpCBox.getSelectedIndex();  <-- will I get the index what the JComboBox had before I make a change?
}
else
{
  TmpCBox = (JComboBox)ie.getSource();
   NowSelect = TmpCBox.getSelectedIndex();
}

Would this work?
0
 
TimYatesCommented:
> if ( ie.getStateChange() == ItemEvent.DESELECTED )  <--- what will this do?

That is the event to tell you an item has been deselected

>   LastSelect = TmpCBox.getSelectedIndex();  <-- will I get the index what the JComboBox had before I make a change?

Yes
0
 
xenia27Author Commented:
LastSelect = TmpCBox.getSelectedIndex();  <-- I got the index I just selected, which is the new information

What's wrong???
0
 
TimYatesCommented:
WHOOOPS!

>  LastSelect = TmpCBox.getSelectedIndex();  <-- will I get the index what the JComboBox had before I make a change?

No...

That will return you the currently selected item...

What you need to do is what objects said:

  for( int i = 0 ; i < TmpCBox.getItemCount() ; i++ )
  {
      if( TmpCBox.getItemAt( i ) == ie.getItem() )
      {
           LastSelect = i ;
           break ;
      }
  }
 
0
 
xenia27Author Commented:
is there an easier way that is not in a for loop??
what will "ie.getItem()" return???
0
 
TimYatesCommented:
>  is there an easier way that is not in a for loop??

As objects said again, if you are using a DefaultComboBoxModel for your items, then you can do

   LastSelect = ((DefaultComboBoxModel)TmpCBox.getModel()).getIndexOf( ie.getItem() ) ;

> what will "ie.getItem()" return???

The item that this event was fired for
0
 
zzynxSoftware engineerCommented:
This demo prints which item is deselected whenever you select another item:

/*
 * ComboDemo.java
 *
 */

import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author  zzynx
 */
public class ComboDemo extends javax.swing.JFrame {
   
    private DefaultComboBoxModel model = null;
    private JComboBox theComboBox;
   
    public ComboDemo() {
        initComponents();
       
        initCombo();
        setSize(200,200);
        setLocationRelativeTo(null);
    }
   
    private void initCombo() {
        model = new DefaultComboBoxModel();
        model.addElement("Item 1");
        model.addElement("Item 2");
        model.addElement("Item 3");
        theComboBox.setModel(model);
    }
   
    private void initComponents() {
        theComboBox = new javax.swing.JComboBox();

        getContentPane().setLayout(new java.awt.FlowLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        theComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                theComboBoxItemStateChanged(evt);
            }
        });

        getContentPane().add(theComboBox);

        pack();
    }

    private void theComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
           System.out.println("Item deselected: " +
                model.getElementAt( model.getIndexOf(evt.getItem()) ) );
        }
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new ComboDemo().show();
    }
   
}
0
 
zzynxSoftware engineerCommented:
If you don't want to *explicitely* use the DefaultComboBoxModel,
then the combination of

    private void initCombo() {
        theComboBox.addItem("Item 1");
        theComboBox.addItem("Item 2");
        theComboBox.addItem("Item 3");
    }

and

    private void theComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
           DefaultComboBoxModel model = (DefaultComboBoxModel)theComboBox.getModel();
           System.out.println("Item deselected: " +
                model.getElementAt( model.getIndexOf(evt.getItem()) ) );
        }
    }

works too.
0
 
xenia27Author Commented:
Great...that's what I need~~~  Thank you~~~~ ^________^
0
 
zzynxSoftware engineerCommented:
>> is there an easier way that is not in a for loop??
Yes, there is.

>> what will "ie.getItem()" return
That just returns what you need.

In my code change theComboBoxItemStateChanged() to:

    private void theComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.DESELECTED) {
           System.out.println("Item deselected: " + evt.getItem() );
        }
    }

0
 
xenia27Author Commented:
Thanks for helping me~~~  ^^
0
 
zzynxSoftware engineerCommented:
Thanks xenia
0
 
TimYatesCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now