Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class CheckBoxDemonstration extends JFrame implements ItemListener{ //As you can see, we are now implementing another listener called ItemListener. This listener is the appropriate listener for check boxes. ActionListener is mostly for buttons. In Java, there are plenty of listeners that you can use.
	
	JLabel label = new JLabel ("What would you like to drink?");
	JLabel information = new JLabel ("");
	ButtonGroup bGroup = new ButtonGroup(); //This statement creates a ButtonGroup object called bGroup which will group our check boxes so users will not be allowed to select more than one item (check box).
	JCheckBox coffee = new JCheckBox("Coffee", false); //This is a check box called Coffee
	JCheckBox cola = new JCheckBox("Cola", false); //This is a check box called Cola
	JCheckBox milk = new JCheckBox("Milk", false); //This is a check box called Milk
	JCheckBox water = new JCheckBox("Water", false); //This is a check box called Water
	
	public CheckBoxDemonstration(){
		super("Checkbox Demonstration"); //This statement names the frame as Checkbox Demonstration as soon as CheckBoxDemonstration object is created in main method.
		setLayout(new FlowLayout());
		label.setFont(new Font("Arial", Font.ITALIC, 22));
		information.setFont(new Font("Arial", Font.ITALIC, 22));
		coffee.addItemListener(this);
		cola.addItemListener(this);
		milk.addItemListener(this);
		water.addItemListener(this);
		
		//The statements below adds coffe, cola, milk and water checkboxes as one by adding them to object bGroup which is a ButtonGroup object.
		bGroup.add(coffee);
		bGroup.add(cola);
		bGroup.add(milk);
		bGroup.add(water);
		
		//The statements below adds the components to our frame.
		add(label);
		add(coffee);
		add(cola);
		add(milk);
		add(water);
		add(information);
	}
	
	public void itemStateChanged(ItemEvent e){ //public void itemStateChanged() is a method from ItemListener interface that we will have to override. Remember that methods inside an interface MUST be overriden. The parameter ItemEvent e is necessary for this matter. e is just a variable name which means you can use any name other than e. Think of it this way: "e variable represents what has occured by the time ItemEvent happened.
		Object source = e.getItem(); //This statement will represent which of the check boxes made an event. Note that a check box is an object that is why we declared an object from Object class itself and we named it "object" which will hold information about what particular check box made an event.
		
		//The following if statements obviously perform identify jobs.
		//The first if statement will simply compare if the source (an object which holds information about a particular check box that made an event) is indeed equals to coffee check box.
		if (source == coffee){ 
			int select = e.getStateChange(); //This statement declares a variable select which holds an integer. e.getStateChange() simpy determines if a coffee check box is selected or deselectd. getStateChange() is a method that will help you determine the state of the check box if it is selected ot not. getStateChange() was called by object e which is what you have declared in ItemEvent e.
			
			if(select == ItemEvent.SELECTED){ //If the check box is indeed selected, information label will display the texts "Enjoy you coffee". Through the help of setText() method called information label itself, we are able to change the text of information label which is empty by default.
				information.setText("Enoy your coffee!"); //This is the actual statement that allows us to set a text for a label. Just use setText() method.
			}
			else{ //If the check box is not selected, we just set the text of information label to empty, none or null. 
				information.setText("");
			}
		}
		
		if (source == cola){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your cola!");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == milk){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your milk!");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == water){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("Enoy your water!");
			}
			else{
				information.setText("");
			}
		}
	}
	
	public static void main (String[] args) {
		final int FRAME_WIDTH = 350;
		final int FRAME_HEIGHT = 150;
		CheckBoxDemonstration frame = new CheckBoxDemonstration();
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	
}


What I have tried:

Java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Shiro_Anime extends JFrame implements ItemListener{ 

	JLabel label = new JLabel ("PICK THE ANIME YOU LIKE");
	JLabel information = new JLabel ("\n");
	ButtonGroup bGroup = new ButtonGroup();
	JCheckBox BlackBullet = new JCheckBox("Black Bullet", false); 
	JCheckBox HighSchoolDxD = new JCheckBox("High School DxD", false); 
	JCheckBox OREIMO = new JCheckBox("OREIMO", false); 
	JCheckBox GuiltyCrown = new JCheckBox("Guilty Crown", false);
	JCheckBox Sakurasou = new JCheckBox("Sakurasou", false);
	JCheckBox AngelBeats = new JCheckBox("Angel Beats", false);
	JCheckBox AccelWorld = new JCheckBox("Accel World", false);
	JCheckBox Nisekoi = new JCheckBox("Nisekoi", false);
	JCheckBox NoGameNoLife = new JCheckBox("No Game No Life", false);

	
	public Shiro_Anime(){
		super("MY FAVORITE ANIME");
		setLayout(new FlowLayout());
		label.setFont(new Font("Arial", Font.ITALIC, 22));
		information.setFont(new Font("Arial", Font.ITALIC, 16));
		BlackBullet.addItemListener(this);
		HighSchoolDxD.addItemListener(this);
		OREIMO.addItemListener(this);
		GuiltyCrown.addItemListener(this);
		Sakurasou.addItemListener(this);
		AngelBeats.addItemListener(this);
		AccelWorld.addItemListener(this);
		Nisekoi.addItemListener(this);
		NoGameNoLife.addItemListener(this);
		
		bGroup.add(BlackBullet);
		bGroup.add(HighSchoolDxD);
		bGroup.add(OREIMO);
		bGroup.add(GuiltyCrown);
		bGroup.add(Sakurasou);
		bGroup.add(AngelBeats);
		bGroup.add(AccelWorld);
		bGroup.add(Nisekoi);
		bGroup.add(NoGameNoLife);
		
		add(label);
		add(BlackBullet);
		add(HighSchoolDxD);
		add(OREIMO);
		add(GuiltyCrown);
		add(Sakurasou);
		add(AngelBeats);
		add(AccelWorld);
		add(Nisekoi);
		add(NoGameNoLife);
		add(information);
	}
	
	public void itemStateChanged(ItemEvent e){ 
			Object source = e.getItem();
		
			if (source == BlackBullet){ 
				
			int select = e.getStateChange(); 
			
			if(select == ItemEvent.SELECTED){ 
				information.setText("You are now watching Black Bullet");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == HighSchoolDxD){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching High School DxD");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == OREIMO){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching OREIMO");
			}
			else{
				information.setText("");
			}
		}
		
		if (source == GuiltyCrown){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Guilty Crown");
			}
			else{
				information.setText("");
			}
			
		}
		if (source == Sakurasou){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Sakurasou");
			}
			else{
				information.setText("");
			}
			
		}
		if (source == AngelBeats){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Angels Beats");
			}
			else{
				information.setText("");
			}
			
		}
		if (source ==AccelWorld){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Accel World");
			}
			else{
				information.setText("");
			}
			
		}
			if (source ==Nisekoi){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching Nisekoi");
			}
			else{
				information.setText("");
			}
			
		}
		if (source ==NoGameNoLife){
			int select = e.getStateChange();
			if(select == ItemEvent.SELECTED){
				information.setText("You are now watching No Game No Life");
			}
			else{
				information.setText("");
			}
			
		}
	}
	
	public static void main (String[] args) {
		final int FRAME_WIDTH = 350;
		final int FRAME_HEIGHT = 200;
		Shiro_Anime frame = new Shiro_Anime();
		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		frame.setVisible(true);
	}
	
}
Posted
Updated 23-Aug-16 3:30am
v2
Comments
Richard MacCutchan 20-Aug-16 11:14am    
Please do not just dump a load of unformatted code here and expect someone to fix it for you. Remove the parts that are not relevant to your program, and add proper details of your problem.

1 solution

Hi, I haven't used your code to test the following, but...

Could you surround your code with a "try/catch" for the exception handling?
You can choose how to handle the exception any way you want in the catch area.

Also, not related to your query, could you use "switch/case" instead of the "if" conditions? this is what I usually do. Maybe it's not appropriate here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900