
{filelink=23}
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;
public class JComboBoxValeur extends JFrame
{
JComboBox myCmb;
public JComboBoxValeur()
{
JPanel mainPanel=new JPanel();
myCmb=new JComboBox();
JButton btnIndex=new JButton("Cliquez ici");
btnIndex.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
// Afficher dans une boîté de dialogue, l'élément sélectionné
JOptionPane.showMessageDialog(null, myCmb.getSelectedItem());
}
}
);
Object jours[]={"Lundi", "Mardi", "Mercredi"
, "Jeudi", "Vendredi", "Samedi"};
for(Object elem:jours)
{
myCmb.addItem(elem);
}
mainPanel.add(myCmb);
mainPanel.add(btnIndex);
add(mainPanel);
}
public static void main (String[] args)
{
JComboBoxValeur frame=new JComboBoxValeur();
frame.setTitle("Exemple de JComboBox");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}