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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
public class Main extends JApplet implements ActionListener
{
private JPanel containerPane ;
private JPanel panelCbx1;
private JPanel panelCbx2;
private JPanel panelCbx3;
public void init()
{
JPanel globalPanel = new JPanel();
globalPanel.setLayout(new BorderLayout());
//3 COMBOS
JComboBox jComboBox1=new JComboBox(new Object[]{"cb1 A" ,"cb1 B" ,"cb1 C"});
JComboBox jComboBox2=new JComboBox(new Object[]{"cb2 AA" ,"cb2 BB" ,"cb2 CC"});
JComboBox jComboBox3=new JComboBox(new Object[]{"cb3 AAA","cb3 BBB","cb3 CCC"});
JPanel panelNord = new JPanel();
panelNord.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
//Pre selection dans un combo 2 facon pour index ...
jComboBox1.setSelectedIndex(0);
jComboBox2.setSelectedIndex(1);
// ... ou par item
jComboBox3.setSelectedItem("cb3 CCC");
//pour lancer actionPerformed apres clic dessus
jComboBox1.addActionListener(this);
jComboBox2.addActionListener(this);
jComboBox3.addActionListener(this);
//retrouver d'ou vient l'action
jComboBox1.setActionCommand("1");
jComboBox2.setActionCommand("2");
jComboBox3.setActionCommand("3");
panelNord.add(jComboBox1);
panelNord.add(jComboBox2);
panelNord.add(jComboBox3);
containerPane = new JPanel();
containerPane.setBorder(BorderFactory.createTitledBorder("Pannneau changeant"));
containerPane.setLayout(new BorderLayout(3,1));
panelCbx1 = new JPanel1("rien de neuf pour cb1");
panelCbx2 = new JPanel2("rien de neuf pour cb2");
panelCbx3 = new JPanel3("rien de neuf pour cb3");
containerPane.add(panelCbx1);
globalPanel.add(panelNord,BorderLayout.NORTH);
globalPanel.add(containerPane,BorderLayout.CENTER);
//Le tout dans un scollpane
this.getContentPane().add(new JScrollPane(globalPanel));
}
public void actionPerformed(ActionEvent e)
{
//detection du bouton cliqué et envoi commande change panneau
JComboBox cb = (JComboBox)e.getSource();
String chaine = cb.getSelectedItem().toString();
switch(Integer.parseInt(e.getActionCommand()))
{
case 1:
((ChangeAble)panelCbx1).setChange(chaine);
changePanel(1);
break;
case 2:
((ChangeAble)panelCbx2).setChange(chaine);
changePanel(2);
break;
case 3:
((ChangeAble)panelCbx3).setChange(chaine);
changePanel(3);
break;
}
}
public void changePanel(int index)
{
//Suppression du contenu
containerPane.removeAll();
//creation d'un nouveau contenu
Component cp = null;
switch(index)
{
case 1:
cp=panelCbx1;
break;
case 2:
cp=panelCbx2;
break;
case 3:
cp=panelCbx3;
break;
}
//Ajout et mise a jour
containerPane.add(cp,BorderLayout.NORTH);
containerPane.revalidate();
containerPane.repaint();
}
}
class JPanel1 extends JPanel implements ChangeAble
{
private JLabel changement;
public JPanel1(String title)
{
setBorder(BorderFactory.createTitledBorder("Panneau de type Panel1"));
setLayout(new BorderLayout());
setBackground(new Color(210,210,210));
changement = new JLabel("Rien dans mon panneau 1");
this.add(new JLabel("Affichage des changements : "),BorderLayout.NORTH);
this.add(changement,BorderLayout.CENTER);
}
public void setChange(String chaine)
{
changement.setText("Change="+chaine);
}
}
class JPanel2 extends JPanel implements ChangeAble
{
private JLabel changement;
public JPanel2(String title)
{
setBorder(BorderFactory.createTitledBorder("Panneau de type Panel2"));
setLayout(new BorderLayout());
setBackground(new Color(190,190,190));
changement = new JLabel("Rien dans mon panneau 2");
this.add(new JLabel("Affichage des changements : "),BorderLayout.NORTH);
this.add(changement,BorderLayout.CENTER);
}
public void setChange(String chaine)
{
changement.setText("Change="+chaine);
}
}
class JPanel3 extends JPanel implements ChangeAble
{
private JLabel changement;
public JPanel3(String title)
{
setBorder(BorderFactory.createTitledBorder("Panneau de type Panel3"));
setLayout(new BorderLayout());
setBackground(new Color(230,230,230));
changement = new JLabel("Rien dans mon panneau 3");
this.add(new JLabel("Affichage des changements : "),BorderLayout.NORTH);
this.add(changement,BorderLayout.CENTER);
}
public void setChange(String chaine)
{
changement.setText("Change="+chaine);
}
}
interface ChangeAble
{
public void setChange(String chaine);
} |