As discussed on this forum, I'm not looking for a quick answer, just an explanation on why what I have written is not working and a point in the right direction that will make this program work.

Problem: Purpose of this program is to create a GUI that presents to the user choices for ordering a pizza. The gui portion of the code works just fine.
Problem: When I press the 'calculate' button, nothing shows up on the result textfield box.
Problem: I can't figure out the necessary code that will clear all options for the 'clear' button.

Any guidance provided is much appreciated.

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

public class Program2 extends JFrame implements ActionListener
{
 //create 5 sections of frame organized by borderlayout
 JPanel westPanel = new JPanel();
 JPanel eastPanel = new JPanel();
 JPanel centerPanel = new JPanel();
 JPanel northPanel = new JPanel();
 JPanel southPanel = new JPanel();
 
 //pizza size options
 JRadioButton jrbSmall = new JRadioButton("S");
 JRadioButton jrbMedium = new JRadioButton("M");
 JRadioButton jrbLarge = new JRadioButton("L");
 
 //pizza crust options
 JRadioButton jrbOriginal = new JRadioButton("Original");
 JRadioButton jrbThin = new JRadioButton("Thin");
 JRadioButton jrbPan = new JRadioButton("Pan");
 
 //Delivery or Pickup buttons
 JRadioButton jrbDelivery = new JRadioButton("Delivery");
 JRadioButton jrbPickup = new JRadioButton("Pick-up");
 
 //Drink choices
 JCheckBox jchkCoke = new JCheckBox("Coke");
 JCheckBox jchkDCoke = new JCheckBox("Diet Coke");
 JCheckBox jchkDrPepper = new JCheckBox("Dr Pepper");
 
 //Topping choices
 JCheckBox jchkCheese = new JCheckBox("Cheese");
 JCheckBox jchkPepperoni = new JCheckBox("Pepperoni");
 JCheckBox jchkSausage = new JCheckBox("Sausage");
 JCheckBox jchkChicken = new JCheckBox("Chicken");
 
 //Calculate, Clear, Text Field areas
 JButton jbtCalculate = new JButton("Calculate");
 JButton jbtClear = new JButton("Clear");
 JLabel label = new JLabel("Total: ");
 JTextField tfResult = new JTextField(8);
 
 
 //variables used to calculate total cost
 double drinkCost = 0.00; 
 int toppingQty = 0; 
 double totalCost = 0.00;
 double pizzaSize = 0.00;
 double toppingCost = 0.00;
 double pickupType = 0.00;


 public Program2()
 {
  //layout and description of west portion of frame
  westPanel.setLayout(new GridLayout(2,3));
  westPanel.setBorder(new TitledBorder("Size & Crust Type"));
    
  //register listeners with buttons
  jrbSmall.addActionListener(this);
  jrbMedium.addActionListener(this);
  jrbLarge.addActionListener(this);

  //button group forces radio buttons to act like radio buttons
  ButtonGroup drinkSize = new ButtonGroup();
  drinkSize.add(jrbSmall);
  drinkSize.add(jrbMedium);
  drinkSize.add(jrbLarge);
  
  //button group for pizza crust options
  ButtonGroup pizzaCrust = new ButtonGroup();
  pizzaCrust.add(jrbOriginal);
  pizzaCrust.add(jrbThin);
  pizzaCrust.add(jrbPan);
    
  //adds all buttons to west panel
  westPanel.add(jrbSmall);
  westPanel.add(jrbMedium);
  westPanel.add(jrbLarge);
  westPanel.add(jrbOriginal);
  westPanel.add(jrbThin);
  westPanel.add(jrbPan);
  
  //sets layout and desription for east portion
  eastPanel.setLayout(new GridLayout(2,3));
  eastPanel.setBorder(new TitledBorder("Beverage & Delivery"));
    
  //creates button group for delivery choices then adds buttons to the panel
  ButtonGroup deliveryType = new ButtonGroup();
  deliveryType.add(jrbDelivery);
  deliveryType.add(jrbPickup);
  
  eastPanel.add(jchkCoke);
  eastPanel.add(jchkDCoke);
  eastPanel.add(jchkDrPepper);
  eastPanel.add(jrbDelivery);
  eastPanel.add(jrbPickup);
  
  //sets layout and description for center portion
  centerPanel.setLayout(new GridLayout(5,5));
  centerPanel.setBorder(new TitledBorder("Toppings"));
  
  //adds buttons to center portion of panel
  centerPanel.add(jchkCheese);
  centerPanel.add(jchkPepperoni);
  centerPanel.add(jchkSausage);
  centerPanel.add(jchkChicken);
  
  northPanel.add(new JButton("Matt's Pizza Parlor"));

  southPanel.add(jbtCalculate);
  southPanel.add(jbtClear);
  southPanel.add(label);
  southPanel.add(tfResult);
  
  //adds all portions of panel together
  setLayout(new BorderLayout(5,5));
  add(westPanel, BorderLayout.WEST);
  add(eastPanel, BorderLayout.EAST);
  add(centerPanel, BorderLayout.CENTER); 
  add(northPanel, BorderLayout.NORTH);
  add(southPanel, BorderLayout.SOUTH);
 }
  
  //handles action event for each of the pizza options
  //setSize<S,M,L> calls method and assigns correct price
  //setPickup/setDelivery calls method and assigns correct price
  //drinkCost keeps a rally total of total drinks chosen
  //toppingQty keeps a rally total of toppings chosen then
  //calls the toppingCost method to determine cost of toppings
  
  public void actionPerformed(ActionEvent e) 
  {
    if (e.getSource() == jrbSmall)
     setSizeS();
	 else if (e.getSource() == jrbMedium)
	  setSizeM();
	 else if (e.getSource() == jrbLarge)
	  setSizeL();   
	 
	 if (e.getSource() == jrbPickup)
     setPickup();
	 else if (e.getSource() == jrbDelivery)
	  setDelivery();
	 
	 if (e.getSource() == jchkCoke)
	  drinkCost++;
	 else if (e.getSource() == jchkDCoke)
	  drinkCost++;
	 else if (e.getSource() == jchkDrPepper)
	  drinkCost++;	      
	 
	 if (e.getSource() == jchkCheese)
	  toppingQty++;
	 else if (e.getSource() == jchkPepperoni)
	  toppingQty++;
	 else if (e.getSource() == jchkSausage)
	  toppingQty++;
	 else if (e.getSource() == jchkChicken)
	  toppingQty++;
	  
	 toppingCost(toppingQty);
	 
	 if (e.getSource() == jbtCalculate)
	   calculate(pizzaSize, toppingCost, pickupType, drinkCost);
  }
  
  public double setSizeS()
  {
	return pizzaSize = 5.00;
  }
  
  public double setSizeM()
  {
	return pizzaSize = 10.00;
  }
  
  public double setSizeL()
  {
   return pizzaSize = 12.00;
  }
  
  public double setPickup()
  {
	return pickupType = 0.0;
  }
  
  public double setDelivery()
  {
	return pickupType = 1.25;
  }
  
  public double toppingCost(int toppingQty)
  {
	if (toppingQty == 1)
	  toppingCost = 0.50;
	else if (toppingQty <= 3)
	  toppingCost = 1.00;
	else if (toppingQty > 3)
	  toppingCost = 1.50;
	
	return toppingCost;
  }
  
  //method for determining total cost of pizza order by combining results from
  //choices chosen in gui
  public void calculate(double pizzaSize, double toppingCost, double pickupType, double drinkCost)
  {
   totalCost = pizzaSize + toppingCost + pickupType + drinkCost;
	tfResult.setText(String.valueOf(totalCost));
  }

  //main method for creating frame info
  public static void main(String[] args)
  {
	 Program2 jm = new Program2();
	 //set the window title
    jm.setTitle("JAVA ITP220 - Program 2");
	 //set the window location
    jm.setLocationRelativeTo(null);
	 //specify what happens when the close button is clicked
    jm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 //set the window size
    jm.setSize(800, 250);
	 //display the window
    jm.setVisible(true);
  }
 }

1. you did not include an actiontionListener() to your Calculate button. You should have:

jbtCalculate.addActionListener(this);

2. same as above you need to add an actionListener to the "clear" button. then you could make a method which clears all the selection and reset the total cost by using setSelected(false) methods of each components you used(ie radio buttons, checkboxes).

hope that helps :)

Thank you! I'm glad to find out that what I had written wasn't completely off track - and it looks as though I missed quite a few actionListeners.

With regards to the clear button. With your help, I got the textfield reset, but I need some additional guidance for clearing the selected radio and check buttons.

checkButton.setSelected(false); should work. In any case there are javadocs for all these things. Type the class name into google, click the javadoc, and read about the methods.

Edited 7 Years Ago by BestJewSinceJC: n/a

for clearing the checkboxes simply use:

//Topping choices
jchkCheese.setSelected(false);
jchkPepperoni.setSelected(false);
jchkSausage.setSelected(false);
//do this for your other checkboxes

do this for all the checkboxes.
For your radio buttons, you are on the right track by grouping them however you need to make this as a global variable so that you can use it for the clearing method then use that group to clear your selection.

drinkSize.clearSelection();
//do this for your other button groups

I see that button_name.setSelected(false) works for checkboxes, but not for radio buttons.
Is there no way to reset radio buttons to an empty set or does it need to be written so that it somehow changes to a default button?

I have one last question ...

I'm trying to add a bit of code that will put a pizza image in the northpanel, but I'm getting an error, "cannot find symbol", but everything looks to spelled correctly.

northPanel.add(new JButton("Matt's Pizza Parlor"));
  ImageIcon image = new ImageIcon("pizza.jpeg");
  northPanel.add(image);

I see that button_name.setSelected(false) works for checkboxes, but not for radio buttons.
Is there no way to reset radio buttons to an empty set or does it need to be written so that it somehow changes to a default button?

The best way is to read the documentation. That way, you can learn to code on your own rather than needing to ask for things like that. I'm not saying that because I'm mad because you asked, or anything like that, but because reading the documentation for these kinds of issues is simple and is useful to be able to do.

Here you can find info about JRadioButton and how to use it.
http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#buttongroup

Here you can find the API doc for JRadioButton
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JRadioButton.html

As for your segment of code that you posted above, are you sure that you can add an ImageIcon to a JPanel? Because I don't see any methods for doing so. I'm not saying it's impossible, but where is the "add" constructor that takes an ImageIcon? All of JPanel's add constructors take Components. Since an ImageIcon is not a Component, I don't think that will work.

http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/LoadimagetoImageIconandaddittoPanel.htm

Edited 7 Years Ago by BestJewSinceJC: n/a

I have read the documentation, was just getting confused on the implementation, specifically on utilizing the button group instead of the individual buttons. Through the help of posters here, I see not just the how but the why as well.

With regards to the imageIcon portion, I'm still hitting a wall, despite realizing that only components get added to the jpanel. I created a button to hold the image so that I could add that to the panel, but the button shows blank.

I have 2 lines of code:

//this is declared globally
ImageIcon image = new ImageIcon("C:\\Users\\Matt Cofsky\\Documents\\Java 220\\pizza.jpeg");

//this is with rest of code outlined above
northPanel.add(new JButton(image));

Edited 7 Years Ago by tnccjavaMatt: n/a

This article has been dead for over six months. Start a new discussion instead.