Saturday, August 24, 2013

JCheckBox

JCheckBox represents a check box item that can be turned on or turned off. You can turn on or turn off a number of items. The JCheckBox component can generate item event so you need to use the ItemListener interface to receive the event. Alternatively, you also can use the AbstractAction class that implements the Action Interface to handle the action event that is generated by the JCheckBox as you can do with JButton.

Constructors of the JCheckBox class:
-JCheckBox()
creates a new checkbox.
-JCheckBox(String text)
creates a new checkbox with specified text.
-JCheckBox(String text, boolean state)
creates a new checkbox with specified text and selection state.
-JCheckBox(Action action)
creates a new checkbox with properties taken from the implementation of the Action interface.
-JCheckBox(Icon icon)
creates a new CheckBox with an image icon.
-JCheckBox(Icon icon, boolean state)
creates a new CheckBox with an image icon and specified selection state.
-JCheckBox(String text,Icon icon)
creates a new checkbox with specified text and image icon.

Useful methods of the JCheckBox class:
-getText():String
returns the text of the checkbox.
-isSelected():boolean
determines whether or not the checkbox is selected.
-setText(String text)
sets the text of the checkbox.

Example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicBorders;

class JCheckBoxShow extends JFrame implements ItemListener{
JLabel lbl;
JCheckBoxShow(String title){
setTitle(title);
setSize(400,300);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
String[] hobbies={"Flower arranging","Mountain biking","Card playing","Football watching","Game playing"};
JPanel p=new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
p.setBorder(new BasicBorders.ButtonBorder(Color.RED,Color.BLACK,Color.GREEN,Color.YELLOW));
contpane.add(new JLabel("What do you do in your free time?"), BorderLayout.NORTH);
for(String h:hobbies){
p.add(new JCheckBox(h));

}
for(int i=0;i<p.getComponentCount();i++)
{
JCheckBox ch=(JCheckBox)p.getComponent(i);
ch.addItemListener(this);
}
contpane.add(p,BorderLayout.CENTER);
lbl=new JLabel();
contpane.add(lbl,BorderLayout.SOUTH);
setVisible(true);

}

public void itemStateChanged(ItemEvent e){
JCheckBox ch=(JCheckBox)e.getSource();
if(ch.isSelected())
lbl.setText(lbl.getText()+" "+ch.getText());
else
lbl.setText(lbl.getText().replaceAll(ch.getText(),""));
}

}

public class JFrameSwing {
public static void main(String[] args){
new JCheckBoxShow("JCheckBox");
}
}

JCheckBox Swing

To get the number of components added to the container, you can use its getComponentCount() method. If you want to visit every component of the container, you will need to use a loop. Each component in the container can be retrieved by using its getComponent(int index) method.

No comments:

Post a Comment