Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problems passing an object through private classes

setpreferredsize

  • Please log in to reply
1 reply to this topic

#1 9erNumber16

9erNumber16

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 25 February 2011 - 09:01 PM

I keep getting a Null exception after trying to submit data entered by radio buttons. It appears the setters aren't working in my radio button listeners, and I'm not sure why.

// ***********************************
// OrderPanel.java
//
// Creates an interface for ordering
// objects from a restaurant, using
// radio buttons and a JButton.
//
// ***********************************

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

public class OrderPanel extends JPanel
{
	// creating graphical components
	private JRadioButton pizza, beverage, mozzSticks, breadSticks, small, medium, large;
	private JLabel totalPrice;
	private JButton order;
	
	private static int count = 0;
	private static int total = 0;
	
	public static FoodItem orderUp = new FoodItem();
	
	public OrderPanel()
	{
		
		totalPrice = new JLabel("Total ");
		
		order = new JButton("Order");
		ButtonListener buttonListen = new ButtonListener();
		order.addActionListener(buttonListen);
	
		// all the items available for order
		pizza = new JRadioButton("Pizza", true);
		pizza.setBackground(Color.red);
		beverage = new JRadioButton("Beverage");
		beverage.setBackground(Color.red);
		mozzSticks = new JRadioButton("Mozzarella Sticks");
		mozzSticks.setBackground(Color.red);
		breadSticks = new JRadioButton("Breadsticks");
		breadSticks.setBackground(Color.red);
	
		// grouping order items
		ButtonGroup items = new ButtonGroup();
		items.add(pizza);
		items.add(beverage);
		items.add(mozzSticks);
		items.add(breadSticks);
		
		// listening
		ItemListener itemListen = new ItemListener();
		pizza.addActionListener(itemListen);
		beverage.addActionListener(itemListen);
		mozzSticks.addActionListener(itemListen);
		breadSticks.addActionListener(itemListen);
	
		// size of item to be ordered
		small = new JRadioButton("Small", true);
		small.setBackground(Color.red);
		medium = new JRadioButton("Medium");
		medium.setBackground(Color.red);
		large = new JRadioButton("Large");
		large.setBackground(Color.red);
		
		SizeListener sizeListen = new SizeListener();
		small.addActionListener(sizeListen);
		medium.addActionListener(sizeListen);
		large.addActionListener(sizeListen);
	
		ButtonGroup sizes = new ButtonGroup();
		sizes.add(small);
		sizes.add(medium);
		sizes.add(large);
		
		// adding all the radio buttons and the order button to the frame
		add(pizza);
		add(beverage);
		add(mozzSticks);
		add(breadSticks);
		
		add(small);
		add(medium);
		add(large);
		
		add(totalPrice);
		add(order);
		
		setBackground(Color.RED);
		setPreferredSize(new Dimension (400, 100));
		
		
	}
	
	// listener for items to be ordered
	private class ItemListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			Object source = event.getSource();
			
			// sorting out what has been selected
			if (source == pizza)
			{
				orderUp.setItem("Pizza");
			}
			else if (source == beverage)
			{
				orderUp.setItem("Beverage");
			}
			else if (source == mozzSticks)
			{
				orderUp.setItem("Mozzarella Sticks");
			}
			else if (source == breadSticks)
			{
				orderUp.setItem("Breadsticks");
			}
		}
		
	}
	
	// listener for sizes
	private class SizeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			Object source = event.getSource();
			
			// sorting out size selected
			if (source == small)
			{
				orderUp.setSize("Small");
			}
			else if (source == medium)
			{
				orderUp.setSize("Medium");
			}
			else if (source == large)
			{
				orderUp.setSize("Large");
			}
		}
	}
	
	// listener for the Order button
	private class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			int tempPrice = 0;

			// sets prices according to what the radio buttons have selected
			if (orderUp.getItem().equals("Pizza"))
			{
				if (orderUp.getSize().equals("Small"))
				{
					tempPrice = 7;
				}
				else if (orderUp.getSize().equals("Medium"))
				{
					tempPrice = 10;
				}
				else if (orderUp.getSize().equals("Large"))
				{
					tempPrice = 15;
				}
			}
			else if (orderUp.equals("Beverage"))
			{
				if (orderUp.getSize().equals("Small"))
				{
					tempPrice = 2;
				}
				else if (orderUp.getSize().equals("Medium"))
				{
					tempPrice = 3;
				}
				else if (orderUp.getSize().equals("Large"))
				{
					tempPrice = 4;
				}
			}
			else if (orderUp.equals("Mozzarella Sticks"))
			{
				if (orderUp.getSize().equals("Small"))
				{
					tempPrice = 5;
				}
				else if (orderUp.getSize().equals("Medium"))
				{
					tempPrice = 7;
				}
				else if (orderUp.getSize().equals("Large"))
				{
					tempPrice = 9;
				}
			}
			else if (orderUp.equals("Breadsticks"))
			{
				if (orderUp.getSize().equals("Small"))
				{
					tempPrice = 4;
				}
				else if (orderUp.getSize().equals("Medium"))
				{
					tempPrice = 6;
				}
				else if (orderUp.getSize().equals("Large"))
				{
					tempPrice = 8;
				}
			}
			
			// adds the price of the individual order to the total cost of the order
			total += tempPrice;
			totalPrice.setText("Total: " + total);
			count++;
		}
	}
}

  • 0

#2 9erNumber16

9erNumber16

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 26 February 2011 - 12:30 AM

Never mind, figured it out.
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download