Les événements dans le jcomboBox

Marion -  
 marion -
Je place un jcomboBox1 contenant par exemple des formations.
Quand on sélectionne une formation, je remplis un 2ème jcomboBox2 avec les noms des formateurs pouvant effectuer la formation.
Quand on sélectionne un formateur, je veux enregistrer le formateur affecté à la formation.

Mais... quand on sélectionne une formation dans jcomboBox1, et qu'on remplit le jcomboBox2 avec un additem de formateurs, cela déclenche l'événement de sélection dans jcomboBox2, ce que je ne veux pas.
J'ai tout testé, actionPerformed de ActionListener, itemStateChanged de ItemListener avec getchange

Avez-vous la solution?
Voici lafenetre.java :

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import java.awt.Rectangle;
import java.util.Vector;

public class lafenetre extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JComboBox jComboBox1 = null;
private JComboBox jComboBox2 = null;
public lafenetre() throws HeadlessException {
// TODO Auto-generated constructor stub
super();
initialize();
}
public lafenetre(GraphicsConfiguration arg0) {
super(arg0);
// TODO Auto-generated constructor stub
initialize();
}
public lafenetre(String arg0) throws HeadlessException {
super(arg0);
// TODO Auto-generated constructor stub
initialize();
}
public lafenetre(String arg0, GraphicsConfiguration arg1) {
super(arg0, arg1);
// TODO Auto-generated constructor stub
initialize();
}

/**
* This method initializes jComboBox1
*/
private JComboBox getJComboBox1() {
if (jComboBox1 == null) {
jComboBox1 = new JComboBox();
jComboBox1.addItem("Formation1");
jComboBox1.addItem("Formation2");
jComboBox1.setBounds(new Rectangle(21, 36, 94, 17));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (jComboBox2.getItemCount()>0){
jComboBox2.removeAllItems();}

//String ch1;

//ch1= (String)jComboBox1.getSelectedItem();
if (jComboBox1.getSelectedItem().equals("Formation1"))
{ System.out.println("actionPerformed() sur jCombo1");
jComboBox2.addItem("Dupont");
jComboBox2.addItem("Legrand");
jComboBox2.addItem("Xénon");
System.out.println("actionPerformed() sur jCombo1");
}
else
{jComboBox2.addItem("Lemineur");
jComboBox2.addItem("Leboulanger");
System.out.println("actionPerformed() sur jCombo1"); }
}
});
}
return jComboBox1;
}

/**
* This method initializes jComboBox2
*/
private JComboBox getJComboBox2() {
if (jComboBox2 == null) {
jComboBox2 = new JComboBox();
jComboBox2.setBounds(new Rectangle(162, 61, 103, 24));
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (jComboBox2.getSelectedIndex()==-1)
{int option1 = JOptionPane.showConfirmDialog(null,"null", "Gestion du centre de formation", JOptionPane.OK_CANCEL_OPTION);
System.out.println("dans jcombo2 : null");
}
else
{int option2 = JOptionPane.showConfirmDialog(null,"Enregistrement du formateur "+jComboBox2.getSelectedItem(), "Gestion du centre de formation",JOptionPane.OK_CANCEL_OPTION);
System.out.println("dans jcombo2 "+jComboBox2.getSelectedItem());
}
}
});
}
return jComboBox2;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lafenetre thisClass = new lafenetre();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This method initializes this
* @return void */
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
* @return javax.swing.JPanel */
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox1(), null);
jContentPane.add(getJComboBox2(), null);
}
return jContentPane;
}

2 réponses

Jo
 
si tu as la réponse je suis prenneur ! Merci
0
marion
 
il faut gérer soi-même l'événemnent du jcombobox2.
je mets en variable globale un booléen "traitement" que j'initialise à false avant le remplissage du jcombobox2. après le remplissage du jcombobox2 je le mets à true.
dans l'événement du jcombobox2, je teste "traitement" pour savoir si je dois effectivement faire le traitement ou non
0