JComboBox liée ?

trungnguyen Messages postés 1 Date d'inscription samedi 15 mai 2010 Statut Membre Dernière intervention 2 décembre 2010 - 2 déc. 2010 à 22:19
amundain Messages postés 110 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 28 août 2012 - 3 déc. 2010 à 11:56
Bonjour,
J'ai deux JComboBox, le premier JComboBox contient deux catégories et pour chaque catégories on a deux sous-catégories qui sont indiquées dans le deuxième JComboBox.

En gros, deux catégories sont "Choix 1" et "Choix 2".
+ Si le premier JComboBox est "Choix 1", on a deux choix "int 1" et "int 2" dans le deuxième JComboBox.
+ Si le premier JComboBox est "Choix 2", on a deux choix "float 1" et "float 2" dans le deuxième JComboBox.

Voici mon code, mais ça fonctionne pas comme je le pense:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;


public class Fenetre extends JFrame implements ActionListener
{
private JComboBox listeA;
private JComboBox listeB;
private ArrayList<String> tabA,tabB1,tabB2;

public Fenetre()
{	
this.setTitle("My principal window");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(500,100);
this.setVisible(true);

Container contenu = this.getContentPane();
contenu.setLayout(new FlowLayout());

this.tabA = new ArrayList<String>();
this.tabA.add(new String("Choix 1"));
this.tabA.add(new String("Choix 2"));

this.listeA = new JComboBox();
for(String a : this.tabA)
this.listeA.addItem(a);

this.tabB1 = new ArrayList<String>();
this.tabB1.add(new String("int 1"));
this.tabB1.add(new String("int 2"));

this.tabB2 = new ArrayList<String>();
this.tabB2.add(new String("float 1"));
this.tabB2.add(new String("float 2"));

this.listeB = new JComboBox();
this.listeB.setEditable(true);
this.listeB.setSelectedItem(new String("Attend !"));

contenu.add(this.listeA);
contenu.add(this.listeB);

this.listeA.addActionListener(this);
this.listeB.addActionListener(this);

this.setVisible(true);
}

public void actionPerformed(ActionEvent e) 
{	
this.listeB.removeAllItems();

String a = new String("Choix 1");
String b = new String("Choix 2");

int j=0;

if(a.equals((String)this.listeA.getSelectedItem()))
{	
for(int i=0;i<this.tabB1.size();i++)
{	
j=j+1;
this.listeB.addItem(this.tabB1.get(i));
}
}

if(b.equals((String)this.listeA.getSelectedItem()))
{
for(int i=0;i<this.tabB2.size();i++)
{		
this.listeB.addItem(this.tabB2.get(i));
}
}	
}
}


et
public class MaFenetre
{
public static void main(String[] args) 
{
Fenetre a = new Fenetre();
}
}


Merci d'avance pour vos explications.

2 réponses

amundain Messages postés 110 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 28 août 2012 1
3 déc. 2010 à 11:56
Salut,

Il te faut enlever la ligne suivante :

this.listeB.addActionListener(this);

Tu ajoutes un listener sur tes 2 jComboBox.
Quand ListeA change, tu appelles le listener, ListeB est mise à jour, ce qui déclenche un nouvel appel au listener, puisque le contenu a changé !

Le listener ne doit être ajouté que sur la liste qui provoque le rafraîchissement de celle qui lui est lié, pas sur les 2.


A +,

amundain

Java bien,merci.
0
amundain Messages postés 110 Date d'inscription mercredi 28 mars 2007 Statut Membre Dernière intervention 28 août 2012 1
3 déc. 2010 à 11:56
Salut,

Il te faut enlever la ligne suivante :

this.listeB.addActionListener(this);

Tu ajoutes un listener sur tes 2 jComboBox.
Quand ListeA change, tu appelles le listener, ListeB est mise à jour, ce qui déclenche un nouvel appel au listener, puisque le contenu a changé !

Le listener ne doit être ajouté que sur la liste qui provoque le rafraîchissement de celle qui lui est lié, pas sur les 2.


A +,

amundain

Java bien,merci.
0
Rejoignez-nous