Home > Java > Java Program to write an Applet with Check Boxes handling and display output – Q55

Java Program to write an Applet with Check Boxes handling and display output – Q55

January 28, 2009 Leave a comment Go to comments

Q55: Java Program to write an applet, which shows a two set of check boxes. The first set is independent and second set belongs to a checkbox group (like radio buttons). Whichever check box is selected by the user, it should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

abstract class Ap_CheckboxGroupDemo extends Applet implements ItemListener{
 	String msg = "";
 	Checkbox Win98, WinNT;
 	CheckboxGroup cbg;
 	Button Win;
 	MyCheckbox myCheckbox1,myCheckbox2,myCheckbox3;

 	public void init(){
 		Win98 = new Checkbox("Windows 98/Xp");
 		WinNT = new Checkbox("Windows NT/2000");
 		
 		add(Win98);
 		add(WinNT);
 		
 		Win98.addItemListener(this);
 		WinNT.addItemListener(this);
 		
 		cbg = new CheckboxGroup();
 		myCheckbox1 = new MyCheckbox("Item 1",cbg,true);
 		add(myCheckbox1);
 		
 		myCheckbox2 = new MyCheckbox("Item 2",cbg,false);
 		add(myCheckbox2);
 		
 		myCheckbox3 = new MyCheckbox("Item 3",cbg,false);
 		add(myCheckbox3);
	}
	
	public void paint(Graphics g){
		msg = "current state: ";
 		g.drawString(msg,6,80);
 		msg = "Windows 98/Xp:" + Win98.getState();
 		g.drawString(msg,6,100);
 		msg = "Windows NT/2000:" + WinNT.getState();
 	}
 }
 		
 


class MyCheckbox extends Checkbox{
 	
 	public MyCheckbox(String label,CheckboxGroup cbg,boolean flag){
 		super(label,cbg,flag);
 		enableEvents(AWTEvent.ITEM_EVENT_MASK);
	}
	
	protected void processItemEvent(ItemEvent ie){
		showStatus("checkbox name/state: " + getLabel() + 
 					"/" + getState());
 		super.processItemEvent(ie);
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: