Solved

ItemListener and stateChange

Posted on 2004-09-02
18
1,044 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

821 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