Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: JCheckboxes Item Listener

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JCheckboxes Item Listener

    import java.awt.*;
    import javax.swing.*;
     
    import java.awt.event.*;
    public class JPizza extends JFrame implements ItemListener
    {
    	private int pizza = 0;
    	FlowLayout flow = new FlowLayout(); 
    	double toppings = 0;
    	double sizes;
    	double totalprice = toppings + sizes;
    	JLabel label = new JLabel("Please choose the size of pizza you would like and the topping : )"); 
    	JLabel label2 = new JLabel("Your total price is : " + totalprice);
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	JCheckBox pepperoni= new JCheckBox("pepperoni"); 
    	JCheckBox sausage= new JCheckBox("cheese"); 
    	JCheckBox mushrooms= new JCheckBox("mushrooms"); 
    	JCheckBox peppers= new JCheckBox("peppers"); 
    	JCheckBox pineapples= new JCheckBox("pineapples");
    	String[] pizzasize = { "small", "medium", "large", "xlarge"}; 
    	JComboBox size = new JComboBox(pizzasize); 
    	public JPizza() 
    	{ 
    	super("Pizzeria Menu and Prices"); 
    	size.addItemListener(this);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	setLayout (new FlowLayout()); 
    	getContentPane().setBackground(Color.black); 
     
    	label.setFont(new Font("Arial", Font.BOLD, 18)); 
    	label.setForeground(Color.white); 
    	label2.setFont(new Font("Arial", Font.BOLD, 18));
    	label2.setForeground(Color.white); 
    	size.setFont(new Font("Arial", Font.BOLD, 22)); 
    	size.setForeground(Color.white); 
    	size.setBackground(Color.black); 
    	pepperoni.addItemListener(this); 
    	mushrooms.addItemListener(this); 
    	sausage.addItemListener(this); 
    	peppers.addItemListener(this); 
     
    	pineapples.addItemListener(this); 
    	pepperoni.setForeground(Color.white); 
    	sausage.setForeground(Color.white); 
    	mushrooms.setForeground(Color.white); 
    	peppers.setForeground(Color.white); 
    	pineapples.setForeground(Color.white); 
    	pepperoni.setBackground(Color.black); 
    	sausage.setBackground(Color.black); 
    	mushrooms.setBackground(Color.black); 
    	peppers.setBackground(Color.black); 
    	pineapples.setBackground(Color.black); 
     
    	add(pepperoni); 
    	add(sausage); 
    	add(mushrooms); 
    	add(peppers); 
    	add(pineapples); 
    	add(label); 
    	add(size); 
    	setSize(FRAME_WIDTH, FRAME_HEIGHT); 
    	add(label2);
    	}
    	 public void itemStateChanged2(ItemEvent e) {
    		  if (pepperoni.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (sausage.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (mushrooms.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (peppers.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (pineapples.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    	 }
    	   public void itemStateChanged(ItemEvent e) {
    	        if (size.getSelectedItem().equals("small")) {
    	        	sizes =7;
    	        }
    	        if (size.getSelectedItem().equals("medium")) {
    	        	sizes =9;
    	        }
    	        if (size.getSelectedItem().equals("large")) {
    	        	sizes =11;
    	        }
    	        if (size.getSelectedItem().equals("xlarge")) {
    	        	sizes =14;
    	        }
     
    	    }
    		public static void main(String[] args) 
    		{ 
    		JPizza frame = new JPizza(); 
    		frame.setVisible(true); 
     
     
     
    	} 
     
    	}






    // I got everything to not give an error in my code... However, I need to display the total price ,and when i run the program the total price stays 0 instead of changing from what check box is highlighted etc. I need help to display the total price.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,348
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: JCheckboxes Item Listener

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Where are the prices and how are they added up to get a total?
    The adding up needs to be done AFTER the user has made his selections. Not when the program first starts.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JCheckboxes Item Listener

    With the itemlistener, I believe I put if the size is selected in the Jcheckbox the size (small,medium,large,xlarge) is given a value in the if statements.

    the double toppings is given a value of 0. However, if the boxes are selected with check marks then toppings ++ which will add 1. If the box is unhighted it will -1.

    the totalprice is calculated by the other two doubles.

    double toppings and double sizes

    I am sorry for not putting the wrap around it

    If I wanted to add up after the code and display it in the box , how would i do so?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,348
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: JCheckboxes Item Listener

    to add up after the code and display it in the box
    Just like that.
    After the user has made his selection and asked for the total:
    add up the prices
    display the total


    How does the user ask for the total to be computed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JCheckboxes Item Listener

    So, I thought it would calculate as you pick your selections... However, will i have to make a button that once clicked will display the total of the users selection?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,348
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: JCheckboxes Item Listener

    calculate as you pick your selections.
    Yes you could do it that way. The listeners could compute a new total every time they are called.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JCheckboxes Item Listener

    I tried making a button using ActionListener, however I cant use an actionlistener with a itemlistener. Is there a reason for that ,and how would i go about making a button to calculate total.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,348
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: JCheckboxes Item Listener

    You can total the items each time a selection is made.
    You need to see what the code is doing. Add some println statements to the listeners and have them print out the new values that they set for the sizes and toppings.
    You would compute the new total at the end of the listeners and set the text of the label to show the new total.

    I cant use an actionlistener with a itemlistener.
    That shouldn't be a problem. If you get error messages, please copy and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    remedys (December 3rd, 2012)

  10. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: JCheckboxes Item Listener

    i resolved the problem with actionlistener and item listener. I got the total to display and i played around however my toppings arent working. the sizes are working in the code but the toppings are not. When a topping is checked i want the int toppings to add 1 to its value but its not...
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import java.awt.event.*;
    public class JPizza extends JFrame implements ItemListener, ActionListener
    {
    	private int pizza = 0;
    	FlowLayout flow = new FlowLayout(); 
    	int toppings = 1;
    	int sizes;
    	int totalprice;
    	JLabel label = new JLabel("Please choose the size of pizza you would like and the topping : )"); 
    	final int FRAME_WIDTH = 600; 
    	final int FRAME_HEIGHT = 400; 
    	JCheckBox pepperoni= new JCheckBox("pepperoni"); 
    	JCheckBox sausage= new JCheckBox("cheese"); 
    	JCheckBox mushrooms= new JCheckBox("mushrooms"); 
    	JCheckBox peppers= new JCheckBox("peppers"); 
    	JCheckBox pineapples= new JCheckBox("pineapples");
    	String[] pizzasize = { "small", "medium", "large", "xlarge"}; 
    	JComboBox size = new JComboBox(pizzasize); 
    	private JButton button1 = new JButton("Calculate");
     
    	public JPizza() 
    	{ 
    	super("Pizzeria Menu and Prices"); 
    	size.addItemListener(this);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	setLayout (new FlowLayout()); 
    	getContentPane().setBackground(Color.black); 
    	label.setFont(new Font("Arial", Font.BOLD, 18)); 
    	label.setForeground(Color.white); 
     
    	size.setFont(new Font("Arial", Font.BOLD, 22)); 
    	size.setForeground(Color.white); 
    	size.setBackground(Color.black); 
    	pepperoni.addItemListener(this); 
    	mushrooms.addItemListener(this); 
    	sausage.addItemListener(this); 
    	peppers.addItemListener(this); 
     
    	pineapples.addItemListener(this); 
    	pepperoni.setForeground(Color.white); 
    	sausage.setForeground(Color.white); 
    	mushrooms.setForeground(Color.white); 
    	peppers.setForeground(Color.white); 
    	pineapples.setForeground(Color.white); 
    	pepperoni.setBackground(Color.black); 
    	sausage.setBackground(Color.black); 
    	mushrooms.setBackground(Color.black); 
    	peppers.setBackground(Color.black); 
    	pineapples.setBackground(Color.black); 
     
    	add(pepperoni); 
    	add(sausage); 
    	add(mushrooms); 
    	add(peppers); 
    	add(pineapples); 
    	add(label); 
    	add(size); 
    	setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     
    	JPanel panel1 = new JPanel();
    	Container con = getContentPane();
    	con.setLayout(new FlowLayout());
    	con.add(panel1);
    	panel1.add(button1);
    	panel1.setBackground(Color.black);
    	button1.addActionListener(this);
     
    	}
    	 public void itemStateChanged2(ItemEvent e) {
    		 if (pepperoni.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (sausage.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (mushrooms.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (peppers.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    		  if (pineapples.isSelected ()) {
    			  toppings ++;
    		    } else {
    		    	toppings --;
    		    }
    	 }
    	   public void itemStateChanged(ItemEvent e) {
    	        if (size.getSelectedItem().equals("small")) {
    	        	sizes =7;
    	        }
    	        if (size.getSelectedItem().equals("medium")) {
    	        	sizes =9;
    	        }
    	        if (size.getSelectedItem().equals("large")) {
    	        	sizes =11;
    	        }
    	        if (size.getSelectedItem().equals("xlarge")) {
    	        	sizes =14;
    	        }
     
    	    }
    	public void actionPerformed(ActionEvent e)
    	{
    		totalprice = toppings + sizes;
    		JOptionPane.showMessageDialog(null,"This will cost " + totalprice);
    	}
     
    		public static void main(String[] args) 
    		{ 
    		JPizza frame = new JPizza(); 
    		frame.setVisible(true); 
     
     
     
    	} 
     
    	}

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,348
    Thanks
    49
    Thanked 2,203 Times in 2,176 Posts

    Default Re: JCheckboxes Item Listener

    my toppings arent working
    Where does the code handle the events from the "toppings" components?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with printing appropriate information of the item
    By learningjava1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2012, 05:57 AM
  2. JcomboBox showing only one item
    By ishiro in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 31st, 2012, 09:29 AM
  3. Replies: 0
    Last Post: February 12th, 2011, 06:44 PM
  4. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  5. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM