Solved

ItemListener and stateChange

Posted on 2004-09-02
18
1,032 Views
Last Modified: 2008-02-01
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
Comment
Question by:xenia27
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11961986
try:

e.getItem()
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11961993
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
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 11962004
if you're using DefaultComboBox model (or subclass) yuou can get the index using:

int index = model.indexOf(e.getItem());
0
 
LVL 92

Expert Comment

by:objects
ID: 11962013
that should be getIndexOf()

must be beer o'clock :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11962017
> must be beer o'clock :)

Mmmmm....beer o'clock ;-)
0
 

Author Comment

by:xenia27
ID: 11962039
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11962044
> 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
 

Author Comment

by:xenia27
ID: 11962052
LastSelect = TmpCBox.getSelectedIndex();  <-- I got the index I just selected, which is the new information

What's wrong???
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 100 total points
ID: 11962058
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:xenia27
ID: 11962068
is there an easier way that is not in a for loop??
what will "ie.getItem()" return???
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11962080
>  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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962123
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 11962159
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
 

Author Comment

by:xenia27
ID: 11962160
Great...that's what I need~~~  Thank you~~~~ ^________^
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11962175
>> 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
 

Author Comment

by:xenia27
ID: 11962179
Thanks for helping me~~~  ^^
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11962181
Thanks xenia
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11962185
:-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now