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 JComboBoxDel extends JFrame { JComboBox myCmb; public JComboBoxDel() { JPanel mainPanel=new JPanel(); myCmb=new JComboBox(); JButton btnSupprimer=new JButton("Supprimer"); btnSupprimer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { DefaultComboBoxModel cmbModel=(DefaultComboBoxModel)myCmb.getModel(); // Supprimer l'élément sélectionné cmbModel.removeElementAt(myCmb.getSelectedIndex()); } } ); 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) { JComboBoxDel frame=new JComboBoxDel(); frame.setTitle("Exemple de Suppression dans JComboBox"); frame.setSize(200, 100); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setVisible(true); } } |