java - catch value of checkbutton in a specific moment
Frage
I have a dynamic application that need to create a window interface with checkbox. The values of the checkboxes will control the next steps of the program.
So I have that interface with the button and I should select the buttons that I want then press a "RUN" button (in the moment of the choice the program must be waiting). How can I do that?
(I cannot use a server application with html tags. It would be too easy.)
This is my code. The problem is that the program controls the values of the checkboxes before I press the RUN button. So all the button states result in FALSE.
public class JRadiobuttonExample extends JFrame {
private JCheckBox radioButton1;
private JCheckBox radioButton2;
private JCheckBox radioButton3;
private JLabel label;
int i, state[] = {0,0,0,0};
JCheckBox b[];
static ArrayList<Boolean> check=new ArrayList<Boolean>();
JButton altervista=new JButton("RUN");
static int num;
public JCheckBox[] getB() {
return b;
}
public void setB(JCheckBox[] b) {
this.b = b;
}
public ArrayList<Boolean> getCheck() {
return check;
}
public void setCheck(ArrayList<Boolean> check2) {
check = check2;
}
public JRadiobuttonExample(int num, ArrayList<String> lbl) {
super("JRadiobuttonExample");
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
b= new JCheckBox[num];
CheckBoxListener ckListener = new CheckBoxListener();
for(i=0; i<num; i++) {
b[i] = new JCheckBox(lbl.get(i));
b[i].addItemListener(ckListener);
getContentPane().add(b[i]);
}
setVisible(true);
getContentPane().add(altervista);
getContentPane().setVisible(true);
Ascoltatore asc=new Ascoltatore();
altervista.addActionListener(asc);
}
class Ascoltatore extends WindowAdapter implements ActionListener{
public void windowClosing(WindowEvent e){
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==altervista){
}
}
}
public static void main(String argv[])
{
ArrayList<String> col=new ArrayList<String>();
col.add("hello");
col.add("everyone");
num=col.size();
JRadiobuttonExample b = new JRadiobuttonExample(num,col);
JCheckBox[] buttons=b.getB();
for (JCheckBox c:buttons){
check.add(c.isSelected());
}
System.out.println("checkbox values are:");
for (boolean sel:check ){
System.out.println(sel);
}
}
}
Keine korrekte Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow