lundi 5 mars 2018

Adding selected checkbox to array JAVA

Dears,

I will have some test this weekend. And i have some problems when i trying to create a lottery game. I dont know how to store the selected checkbox items to save it out to TXT file.

I need to store the numbers the dates and the name which will be given by the player.

For now i just want to know what is better. Store selected checkbox data or what?

So far i got this:

public  void meg(){
  JPanel megjelenito=new JPanel();
    megjelenito.setSize(400,135);
    megjelenito.setLocation(10,50);
    megjelenito.setLayout(new GridLayout(9,10));
    int melyik=1;
    for(int sor=1;sor<=9;sor++){
        for(int osz=1;osz<=10;osz++){
            JCheckBox szam=new JCheckBox();
            szam.setText(""+melyik);
            melyik++;
            ItemListener il=new ItemListener() {

                @Override
                public void itemStateChanged(ItemEvent e) {
                    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    Kattintas(e);
                }
            };
            szam.addItemListener(il);
            megjelenito.add(szam);
            megjelenito.setVisible(true);
        }
    }
    this.add(megjelenito);
    this.validate();}
 public void Kattintas(ItemEvent melyik){
    JCheckBox cb = (JCheckBox)melyik.getSource();
    System.out.println(cb.isSelected()?cb.getText()+": be":cb.getText()+": ki");




 //  System.out.println(cb.getText()+(cb.isSelected()?": be":": ki"));


    if(cb.isSelected()){
        db++;
    }else{
        db--;
    }
    if(db==5){
        Container k=cb.getParent();
        for(Component k1:k.getComponents()){
            JCheckBox cb1=(JCheckBox)k1;
            if(!cb1.isSelected()){
                cb1.setEnabled(false);


            }
        }
    }else{
        Container k=cb.getParent();
        for(Component k1:k.getComponents()){
            JCheckBox cb1=(JCheckBox)k1;
            if(!cb1.isSelected()){
                cb1.setEnabled(true);
            }
        }            
    }



}

I want to know how to save/store the numbers which are selected in JCheckbox.

Then i want to know how to save data to TXT. But its very secondary :)

Many thanks.




Aucun commentaire:

Enregistrer un commentaire