[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ItemListener and stateChange

Posted on 2004-09-02
18
Medium Priority
?
1,070 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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 400 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

650 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