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
| import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class NamesPanel extends JPanel implements ItemListener {
private static final long serialVersionUID = 1L;
private String utilisateur = "";
public NamesPanel(int nbrCols, String[] names) {
super(new GridLayout(nbrCols, 0));
ButtonGroup group = new ButtonGroup();
JRadioButton current;
for(int i=0; i<names.length; i++)
{
if(i==0)
current = new JRadioButton(names[i], true);
else
current = new JRadioButton(names[i], false);
current.addItemListener(this);
group.add(current);
add(current);
}
}
public void itemStateChanged(ItemEvent evt) {
utilisateur = ((JRadioButton)evt.getSource()).getLabel();
}
public String getUtilisateur() {
return utilisateur;
}
public static void main(String[] args) {
String[] names = new String[] {"TRUC", "MACHIN", "BIDULE", "CHOSE"};
int nbrCols = 2;
NamesPanel myPanel = new NamesPanel(nbrCols, names);
int result = JOptionPane.showOptionDialog(
null,
myPanel,
"QUI ES TU ?",
JOptionPane.YES_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[] {"OK"},
"OK");
if(result == 0)
System.out.println(myPanel.getUtilisateur());
}
} |
Partager