0

When I click a Swing button, I would like to get all selected checkboxes. I tried many methods, such as getSelectedObjects() and getStateChange(). Moreover, I tried to call isSelected() in ItemStateChanged. Finally, I tried to call getSelectedObjects(), but that only worked for one checkbox.

Below is my code. How should I proceed?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.*;
import javax.swing.SwingUtilities.*;

class RegForm extends JFrame implements ItemListener,ActionListener
{
    //component initialize
    JLabel fn=new JLabel("First Name:");
    JLabel ln=new JLabel("Last Name:");
    JLabel ad=new JLabel("Address:");
    JLabel cn=new JLabel("Contact No:");
    JLabel sex=new JLabel("Sex:");
    JLabel sub=new JLabel("Subject:");

    JButton ok=new JButton("Ok");
    JButton can=new JButton("Cancel");

    JRadioButton m=new JRadioButton("Male");
    JRadioButton f=new JRadioButton("Female");
    JRadioButton r1=new JRadioButton("Metal");
    JRadioButton r2=new JRadioButton("Motif");
    JRadioButton r3=new JRadioButton("Window");

    ButtonGroup bglf=new ButtonGroup();

    JCheckBox cl=new JCheckBox("c");
    JCheckBox cpp=new JCheckBox("c++");
    JCheckBox java=new JCheckBox("JAVA");

    TextField tfn=new TextField(10);
    TextField tln=new TextField(10);
    TextArea tadd=new TextArea(10,3);
    TextField tcn=new TextField(10);


    ButtonGroup bg;
    TextArea s=new TextArea(20,5);

    Container c;
    RegForm()
    {
    c=this.getContentPane();
    c.setLayout(null);

    bg=new ButtonGroup();
    bg.add(m);
    bg.add(f);  

    bglf.add(r1);
    bglf.add(r2);
    bglf.add(r3);
    //set position
    fn.setBounds(50,50,100,30);
    ln.setBounds(50,100,100,30);
    ad.setBounds(50,150,100,30);
    cn.setBounds(50,300,100,30);
    sex.setBounds(50,350,100,30);
    sub.setBounds(50,400,100,30);

    tfn.setBounds(200,50,150,30);
    tln.setBounds(200,100,150,30);
    tadd.setBounds(200,150,150,100);
    tcn.setBounds(200,300,150,30);

    ok.setBounds(50,450,100,40);
    can.setBounds(200,450,100,40);

    cl.setBounds(200,400,100,30);
    cpp.setBounds(350,400,100,30);
    java.setBounds(500,400,100,30);

    m.setBounds(200,350,100,30);
    f.setBounds(350,350,100,30);

    s.setBounds(50,500,300,100);    

    r1.setBounds(50,10,100,30);
    r2.setBounds(150,10,100,30);
    r3.setBounds(250,10,100,30);
    //add to container
    c.add(r1);
    c.add(r2);
    c.add(r3);

    c.add(fn);
    c.add(ln);
    c.add(ad);
    c.add(cn);
    c.add(sex);
    c.add(sub);

    c.add(tfn);
    c.add(tln);
    c.add(tadd);
    c.add(tcn);

    c.add(ok);
    c.add(can);
    c.add(cl);
    c.add(cpp);
    c.add(java);

    c.add(m);
    c.add(f);

    c.add(s);   

    m.addItemListener(this);
    f.addItemListener(this);

    cl.addItemListener(this);
    cpp.addItemListener(this);
    java.addItemListener(this);

    ok.addActionListener(this);
    can.addActionListener(this);

    r1.addItemListener(this);
    r2.addItemListener(this);
    r3.addItemListener(this);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public void actionPerformed(ActionEvent ae)
    {
        s.setText(ae.getActionCommand());
        if(ae.getActionCommand().equals("Ok"))
        {
            s.setText(tfn.getText()+"\n"+tln.getText()+"\n"+tadd.getText()+"\n"+tcn.getText());
        }

        if(ae.getActionCommand().equals("Cancel"))
        {
            tfn.setText("");
            tln.setText("");
            tadd.setText("");
            tcn.setText("");
        }
    }
    //JRadioButton rb;
    //JCheckBox cb;
    JToggleButton tb;
    public void itemStateChanged(ItemEvent ie)
    {
        //rb=(JRadioButton)ie.getItem();    
        //cb=(JCheckBox)ie.getItem();
        tb=(JToggleButton)ie.getItem();
        s.setText(tb.getText());


         try
         {
            if(s.equals("Metal"))
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            else if(s.equals("Motif"))
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            else
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(c);
         }
            catch(Exception e)
            {
            }
    }
    public static void main(String ar[])
    {
        RegForm r=new RegForm();
        r.setSize(800,700);
        r.setVisible(true);
    }
}
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
Munir
  • 51
  • 1
  • 9
  • 1
    You're mixing heavy (AWT) and light (Swing) weight components together, this is not recommended. – MadProgrammer Jul 31 '13 at 07:59
  • can you trim your example to a [SSCCE](http://sscce.org/)? – MyPasswordIsLasercats Jul 31 '13 at 08:06
  • It is unclear what the problem is (neither `getSelectedObjetcs()` nor `getStateChange()` appear in the code, so I have no idea what is it that you trying to do that fails). Also, using a `null` layout is a bad idea. Use layout managers instead. – kiheru Jul 31 '13 at 08:11

0 Answers0