Labels

Monday, 4 April 2011

How to Group check boxes - Exercise 14

Please see in this example how India, SA and England are in Checkboxgroup A and the rest in B. You can only select one team in a group (the other team becomes automatically unchecked).

import java.awt.*;
import java.awt.event.*;
/**
*
* @author root
*/
public class CheckBoxGroupDemo extends Frame {

public Checkbox india, srilanka, pakistan, southafrica, england;
public CheckboxGroup groupa, groupb;
public CheckboxListener listen;
public TextField message;
public FlowLayout lay;
public String msg;

public CheckBoxGroupDemo() {



//create textfield
message = new TextField(100);

//create the checkboxgroups
groupa = new CheckboxGroup() ;
groupb = new CheckboxGroup() ;

//create the checkboxes and itemlistener
india = new Checkbox("India",groupa,false);
pakistan = new Checkbox("Pakistan",groupb,false);
srilanka = new Checkbox("Sri Lanka",groupb,false);
southafrica = new Checkbox("South Africa",groupa,false);
england = new Checkbox("England",groupa,false);
listen = new CheckboxListener();
msg = "The action has just begun. Click on the winner.";

//add all checkboxes to the frame
add(india);add(pakistan);add(srilanka);add(southafrica);add(england);

//add the textfield
message.setText(msg);
add(message);

//add the listener to each checkbox
india.addItemListener(listen);pakistan.addItemListener(listen);srilanka.addItemListener(listen);
england.addItemListener(listen);southafrica.addItemListener(listen);

//add window listener to the frame
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

//set the layout, size, visibility and paint the screen with initial message
lay = new FlowLayout();
setLayout(lay);

setSize(200, 100);
setVisible(true);
repaint();
}

public class CheckboxListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
if ( (india.getState()==false) &&
(srilanka.getState()==false) &&
(pakistan.getState()==false) &&
(southafrica.getState()==false) &&
(england.getState()==false)
)
msg = "Please choose candidates to predict";

if (england.getState()==true) {
msg = "England will win.";

}else if (pakistan.getState()==true) {
msg = "Pakistan will win.";
lay.setAlignment(FlowLayout.RIGHT);

}else if (india.getState()==true) {
msg = "India will win.";
lay.setAlignment(FlowLayout.LEADING);


}else if (southafrica.getState()==true) {
msg = "South Africa will win.";
lay.setAlignment(FlowLayout.LEFT);

} else if (srilanka.getState()==true) {
msg = "Srilanka will win.";
lay.setAlignment(FlowLayout.TRAILING);

}

repaint();
}
}

public void paint(Graphics g) {

if (england.getState()==true) {
setBackground(Color.red);
lay.setAlignment(FlowLayout.LEFT);

} else if (pakistan.getState()==true) {
setBackground(Color.GREEN);
lay.setAlignment(FlowLayout.TRAILING);

} else if (india.getState()==true) {
setBackground(Color.blue);

}else if (southafrica.getState()==true) {
setBackground(Color.ORANGE);

}else if (srilanka.getState()==true) {
setBackground(Color.YELLOW);
}
message.setText(msg);

}

public static void main(String[] args) {
CheckBoxGroupDemo predict = new CheckBoxGroupDemo();
}



}

0 comments:

Post a Comment