CheckBox

Résolu
didoux95 Messages postés 845 Date d'inscription mardi 25 avril 2006 Statut Membre Dernière intervention 1 août 2017 - 24 août 2006 à 15:35
Twinuts Messages postés 5374 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 3 mars 2023 - 24 août 2006 à 21:59
bonjour a tous j'aimerais savoir comment on peut cocher/decocher une checkbox.
j'ai essayer ("CB.setState(true);") mais cela m'affiche une erreure.
Y a t-il une astuce ou un autre moyen pour effectuer cet operation?

merci.
A voir également:

8 réponses

Twinuts Messages postés 5374 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 3 mars 2023 111
24 août 2006 à 16:26
Salut,

bon vu que tu n'est pas explicite dans ta demande (swing ou awt) et que tu as du mal sans exemple voilou  :

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public Test(){
        //code SWING
        makeSwing();
        //code AWT
        makeAwt();
       
       
    }
   
    private void makeAwt(){
        Frame f = new Frame("Test Checkbox swing");
        f.addWindowListener(new WindowListener(){//ajout du listener pour la fermeture
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            public void windowClosed(WindowEvent e) {}
            public void windowActivated(WindowEvent e) {}
            public void windowDeactivated(WindowEvent e) {}
            public void windowDeiconified(WindowEvent e) {}
            public void windowIconified(WindowEvent e) {}
            public void windowOpened(WindowEvent e) {}
           
        });
        //groupe de CheckBox
        CheckboxGroup cgp = new CheckboxGroup();
        //creation des check box dans un tableau pour pas me prendre la tete
        Checkbox [] chk = {
                new Checkbox("chk 1", cgp, false),
                new Checkbox("chk 2", cgp, false),
                new Checkbox("chk 3", cgp, true),//on selectionne le 3 pour le moment histoire de
                new Checkbox("chk 4", cgp, false)
        };
        //panel contenant les check box
        Panel contentPane = new Panel();
        //initialisation et ratachement du tout
        for(Checkbox ck : chk)
            contentPane.add(ck);//on ajoute le check box au panel
        //on selectionne le 2eme checkbox
        chk[1].setState(true);
        f.add(contentPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setVisible(true);
    }
   
    private void makeSwing(){
        JFrame f = new JFrame("Test Checkbox swing");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        //creation des check box dans un tableau pour pas me prendre la tete
        JCheckBox [] jchk = {
                new JCheckBox("chk 1"),
                new JCheckBox("chk 2"),
                new JCheckBox("chk 3"),
                new JCheckBox("chk 4")
        };
        //groupe de JCheckBox
        ButtonGroup cgp = new ButtonGroup();
        //panel contenant les check box
        JPanel contentPane = new JPanel();
        //initialisation et ratachement du tout
        for(JCheckBox ck : jchk){
            cgp.add(ck);//on ajout le checkbox au group
            contentPane.add(ck);//on ajoute le check box au panel
        }
        //on selectionne le 3eme checkbox
        jchk[2].setSelected(true);
        f.setContentPane(contentPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }

}

tu peux fermet une fenetre swing ou awt proprement avec frame.dispose();

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

WORA
3