🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #13067

Re: JComboBox ItemStateChanged event fires twice

#13067
From: Richard Wilkman
Date: Thu, 01 Oct 1998 00:00
197 lines
6725 bytes
Michael:

ItemStateChanged is fired twice because state changes twice.

Base.  You have a Combobox (or any other list type item) which by
default has some item selected.

Action.  You select some other item.

Result.  The item that was selected says "hey!  my state changed". 
The item that just got selected says "hey!  me to".

So, inside your event that handles state change check if it's a
selection or a de-selection and act accordingly (usually you just
ignore the de-selection event).

-Rich

Michael J. Olenik wrote:
> 
> When I use the "ItemStateChanged" event in the JCombobox to see if
> another item has been selected, I notice that the event fires TWICE.
> 
> Can anyone lend some insight into why this happens and more importantly
> can it be stopped it from firing twice when the State Changes?
> 
> I have noticed that the "PropertyChange" even fires only once, so maybe
> this is the correct event to look at.
> 
> I have included a sample applet to demonstrate this.
> 
> Thanks
> 
> MIke
> 
> =========== Begin Sample Code ============
> package JComboBox;
> 
> import java.awt.*;
> import java.awt.event.*;
> import java.applet.*;
> import com.sun.java.swing.*;
> import borland.jbcl.layout.*;
> import java.beans.*;
> 
> //import com.sun.java.swing.UIManager;
> public class Applet1 extends JApplet {
>   boolean isStandalone = false;
>   BorderLayout borderLayout1 = new BorderLayout();
>   JPanel jPanel1 = new JPanel();
>   XYLayout xYLayout1 = new XYLayout();
>   JComboBox jComboBox1 = new JComboBox();
>   JComboBox jComboBox2 = new JComboBox();
> 
>   //Construct the applet
> 
>   public Applet1() {
>   }
> //Initialize the applet
> 
>   public void init() {
>     try {
>     jbInit();
>     }
>     catch (Exception e) {
>     e.printStackTrace();
>     }
>     jComboBox1.addItem("jComboBox1");
>     jComboBox1.addItem("Line 1");
>     jComboBox1.addItem("Line 2");
>     jComboBox1.addItem("Line 3");
>     jComboBox1.addItem("Line 4");
>     jComboBox1.addItem("Line 5");
> 
>     jComboBox2.addItem("jComboBox2");
>     jComboBox2.addItem("Line 1");
>     jComboBox2.addItem("Line 2");
>     jComboBox2.addItem("Line 3");
>     jComboBox2.addItem("Line 4");
>     jComboBox2.addItem("Line 5");
> 
>     System.out.println("End of Init");
>   }
> //Component initialization
> 
>   private void jbInit() throws Exception {
>     this.getContentPane().setLayout(borderLayout1);
>     this.setSize(400,300);
>     jPanel1.setLayout(xYLayout1);
>     jComboBox2.addItemListener(new java.awt.event.ItemListener() {
>       public void itemStateChanged(ItemEvent e) {
>         jComboBox2_itemStateChanged(e);
>       }
>     });
>     jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
>       public void mouseClicked(MouseEvent e) {
>         jComboBox1_mouseClicked(e);
>       }
>       public void mouseEntered(MouseEvent e) {
>         jComboBox1_mouseEntered(e);
>       }
>     });
>     jComboBox2.addMouseListener(new java.awt.event.MouseAdapter() {
>       public void mouseClicked(MouseEvent e) {
>         jComboBox2_mouseClicked(e);
>       }
>     });
>     jComboBox1.addPropertyChangeListener(new
> java.beans.PropertyChangeListener() {
>       public void propertyChange(PropertyChangeEvent e) {
>         jComboBox1_propertyChange(e);
>       }
>     });
>     jComboBox1.addVetoableChangeListener(new
> java.beans.VetoableChangeListener() {
>       public void vetoableChange(PropertyChangeEvent e) throws
> PropertyVetoException {
>         jComboBox1_vetoableChange(e);
>       }
>     });
>     jComboBox1.addItemListener(new java.awt.event.ItemListener() {
>       public void itemStateChanged(ItemEvent e) {
>         jComboBox1_itemStateChanged(e);
>       }
>     });
>     this.getContentPane().add(jPanel1, BorderLayout.CENTER);
>     jPanel1.add(jComboBox1, new XYConstraints(28, 40, 227, -1));
>     jPanel1.add(jComboBox2, new XYConstraints(30, 102, 218, -1));
>   }
> //Get Applet information
> 
>   public String getAppletInfo() {
>     return "Applet Information";
>   }
> //Get parameter info
> 
>   public String[][] getParameterInfo() {
>     return null;
>   }
> 
>   void jComboBox1_itemStateChanged(ItemEvent e) {
>     System.out.println("Begin jComboBox1_itemStateChanged");
>     System.out.println("End   jComboBox1_itemStateChanged");
>   }
> 
>   void jComboBox2_itemStateChanged(ItemEvent e) {
>     System.out.println("Begin jComboBox2_itemStateChanged");
>     System.out.println("End   jComboBox2_itemStateChanged");
>   }
> 
>   void jComboBox1_mouseClicked(MouseEvent e) {
>     System.out.println("Begin jComboBox1_mouseClicked");
>     System.out.println("End   jComboBox1_mouseClicked");
> 
>   }
> 
>   void jComboBox2_mouseClicked(MouseEvent e) {
>     System.out.println("Begin jComboBox2_mouseClicked");
>     System.out.println("End   jComboBox2_mouseClicked");
> 
>   }
> 
>   void jComboBox1_mouseEntered(MouseEvent e) {
>     System.out.println("Begin jComboBox1_mouseEntered");
>     System.out.println("End   jComboBox1_mouseEntered");
> 
>   }
> 
>   void jComboBox1_propertyChange(PropertyChangeEvent e) {
>     System.out.println("Begin jComboBox1_propertyChange");
>     System.out.println("End   jComboBox1_propertyChange");
> 
>   }
> 
>   void jComboBox1_vetoableChange(PropertyChangeEvent e) throws
> PropertyVetoException {
>     System.out.println("Begin jComboBox1_vetoableChange");
>     System.out.println("End   jComboBox1_vetoableChange");
> 
>   }
> }
> =========== End Sample Code ============

-- 

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rich Wilkman
Senior Quality Assurance Engineer
Borland JBuilder Team

If you see something that looks like an opinion up there,
it's mine and mine alone.

Mail sent to me from the newsgroups won't bounce but will
be ignored.  If I have time, I'll respond, but you  have
a better chance if you confine your posts to the newsgroups
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Technical Details
Message-ID: <3613FF03.29914FF2@inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36139CE7.85C4A49E@prc.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,b0f282afd51a5eac
X-Google-Attributes: gid111d45,public
From: Richard Wilkman <rwilkman@inprise.com>
Subject: Re: JComboBox ItemStateChanged event fires twice
Date: 1998/10/01
Message-ID: <3613FF03.29914FF2@inprise.com>
X-Deja-AN: 396841566
Content-Transfer-Encoding: 7bit
References: <36139CE7.85C4A49E@prc.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing