
{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 JComboBoxIndex extends JFrame
{
JComboBox listMois;
public JComboBoxIndex()
{
JPanel mainPanel=new JPanel();
listMois=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'indice de l'élément sélectionné
JOptionPane.showMessageDialog(null, listMois.getSelectedIndex());
}
}
);
Object mois[]={"Janvier", "Février", "Mars"
, "Avril", "Mai", "Juin"};
for(Object elem:mois)
{
listMois.addItem(elem);
}
mainPanel.add(listMois);
mainPanel.add(btnIndex);
add(mainPanel);
}
public static void main (String[] args)
{
JComboBoxIndex frame=new JComboBoxIndex();
frame.setTitle("Exemple de JComboBox");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}