Synchronisation de deux JComboBox, vue-Controller
Bonjour à tous,
voila, je rencontre un petit souci, car je n'arrive pas à synchroniser les affichages de deux JComboBox.
Je développe une application de gestion Java/sql server dont l'interface présente 2 combobox, le premier pour la sélection d'un "site" et le second pour la sélection des (de l') affaire(s) concernées.
Pour illustrer comment j'aimerais pouvoir faire fonctionner mes JCombobox, voici 2 classes "factices" mais qui correspondent au fonctionnement que je voudrais obtenir.
D'abord ma classe TestFrame
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class TestFrame {
private static Integer[] choix1={1};
/**
* @param args
*/
public TestFrame(){
}
/*class Beta extends Thread{
JComboBox chooseAff;
Beta(JComboBox chooseAff){
this.chooseAff= chooseAff;
//this.chooseAff.addActionListener(l)
this.start();
}
public void run(){
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
chooseAff= new JComboBox(getChoix1());
}
}
}*/
public static void createAndShowGui(){
JFrame frame = new JFrame("Base PAC");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the MenuBar
Container content;
content= frame.getContentPane();
content.setLayout(new GridLayout(2,2));
JLabel affCombo=new JLabel("SITES");
content.add(affCombo);
String[] listeSites= {"pardi", "monster", "seven", "vista"};
JComboBox chooseSite= new JComboBox(listeSites);
Cont cont= new Cont();// instanciation du contrôleur
chooseSite.setActionCommand("site");
chooseSite.addActionListener(cont);
content.add(chooseSite);
JLabel nameAff= new JLabel("Affaires associées:");
content.add(nameAff);
JComboBox chooseAff=new JComboBox(choix1);
chooseAff.setVisible(true);
chooseAff.setActionCommand("chooseAffaire");
chooseAff.addActionListener(cont);
//TestFrame tF= new TestFrame();
//Beta b= tF.new Beta(chooseAff);
content.add(chooseAff);
//Display the window.
frame.setSize(1024,768);
frame.setVisible(true);
frame.pack();
}
public static Integer[] getChoix1() {
return choix1;
}
public static void setChoix1(Integer[] choix1) {
TestFrame.choix1 = choix1;
}
public static void main(String[] args) {
createAndShowGui();
}
} |
Et voici ma classe Cont(pour controler...excusez la pauvreté du choix des noms...:mur:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
public class Cont implements ActionListener {
private Integer[] choix2={1,2};
private Integer[] choix3={1,2,3};
private Integer[] choix4={1,2,3,4};
private Integer[] choix5={1,2,3,4,5};
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand()=="Site"){
JComboBox cb= (JComboBox)e.getSource();
String siteName=(String) cb.getSelectedItem();
if (siteName =="pardi"){
TestFrame.setChoix1(choix2);
}
else if(siteName =="monster"){
TestFrame.setChoix1(choix3);
}
else if(siteName == "seven"){
TestFrame.setChoix1(choix4);
}
else if(siteName== "vista"){
TestFrame.setChoix1(choix5);
}
}
else if (e.getActionCommand()== "chooseAff"){
System.out.println("Enfin ça fonctionne");
}
} |
Mon problème est que le second combobox ne varie pas en fonction de la sélection du premier. J'ai pensé bien évidemment à la solution des threads, mais je sais pas si je m'y prend mal, en tout cas ça marche toujours pas.
Si vous avez des propositions...je vous remercie:ccool: