Solved

ItemListener and stateChange

Posted on 2004-09-02
18
1,039 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 49
runtime exception 2 27
ejb on wildfly 5 19
String array comparison 4 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

815 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