🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

JComboBox ItemStateChanged event fires twice

#13063
From: Michael J. Olenik
Date: Thu, 01 Oct 1998 00:00
162 lines
5342 bytes
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 ============
Technical Details
Message-ID: <36139CE7.85C4A49E@prc.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

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