import javax.swing.JComboBox; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JOptionPane; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultComboBoxModel; public class JComboBoxClean extends JFrame { JComboBox myCmb; public JComboBoxClean() { JPanel mainPanel=new JPanel(); myCmb=new JComboBox(); JButton btnSupprimer=new JButton("Vider"); btnSupprimer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { DefaultComboBoxModel cmbModel=(DefaultComboBoxModel)myCmb.getModel(); // Vider le JComboBox cmbModel.removeAllElements(); } } ); Object jours[]={"Lundi", "Mardi", "Mercredi" , "Jeudi", "Vendredi", "Samedi"}; for(Object elem:jours) { myCmb.addItem(elem); } mainPanel.add(myCmb); mainPanel.add(btnSupprimer); add(mainPanel); } public static void main (String[] args) { JComboBoxClean frame=new JComboBoxClean(); frame.setTitle("Exemple de Modification dans JComboBox"); frame.setSize(200, 100); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setVisible(true); } } |