Uploaded by Margerie Fruelda

Java Checkbox

advertisement
Introduction
The CheckboxGroup class is used to group the set of checkbox.
Class declaration
Following is the declaration for java.awt.CheckboxGroup class:
public class CheckboxGroup
extends Object
implements Serializable
Class constructors
S.N.
1
Constructor & Description
CheckboxGroup() ()
Creates a new instance of CheckboxGroup.
Class methods
S.N.
1
Method & Description
Checkbox getCurrent()
Deprecated. As of JDK version 1.1, replaced by getSelectedCheckbox().
2
Checkbox getSelectedCheckbox()
Gets the current choice from this check box group.
3
void setCurrent(Checkbox box)
Deprecated. As of JDK version 1.1, replaced by setSelectedCheckbox(Checkbox).
4
void setSelectedCheckbox(Checkbox box)
Sets the currently selected check box in this group to be the specified check box.
5
String toString()
Returns a string representation of this check box group, including the value of its
current selection.
Methods inherited
This class inherits methods from the following classes:

java.lang.Object
CheckBoxGroup Example
Create the following java program using any editor of your choice in say D:/ > AWT >
com > tutorialspoint > gui >
AwtControlDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
public class AwtControlDemo {
private
private
private
private
Frame
Label
Label
Panel
mainFrame;
headerLabel;
statusLabel;
controlPanel;
public AwtControlDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtControlDemo awtControlDemo = new AwtControlDemo();
awtControlDemo.showCheckBoxGroupDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showCheckBoxGroupDemo(){
headerLabel.setText("Control in action: CheckBoxGroup");
CheckboxGroup fruitGroup = new CheckboxGroup();
Checkbox chkApple = new Checkbox("Apple",fruitGroup,true);
Checkbox chkMango = new Checkbox("Mango",fruitGroup,false);
Checkbox chkPeer = new Checkbox("Peer",fruitGroup,false);
statusLabel.setText("Apple Checkbox: checked");
chkApple.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Apple Checkbox: checked");
}
});
chkMango.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Mango Checkbox: checked");
}
});
chkPeer.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
statusLabel.setText("Peer Checkbox: checked");
}
});
controlPanel.add(chkApple);
controlPanel.add(chkMango);
controlPanel.add(chkPeer);
mainFrame.setVisible(true);
}
}
Compile the program using command prompt. Go to D:/ > AWT and type the following
command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following
command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output
Download