4 Replies - 1303 Views - Last Post: 08 December 2012 - 09:07 PM Rate Topic: -----

#1 String lostInClass  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 12

JPanel isn't displaying what I need it to

Posted 07 December 2012 - 07:20 AM

I need to use these 7 classes within my JPanel:

JRadioButton
JComboBox
JList
JTextField
JCheckBox
Jlabel
JTextArea – use to display the user’s selection(s)

Object of program is for user to place an order for their pizza and have their order and price be shown as they go along. I've gotten this far but when I run it, there are no errors, and the panel appears but none of the content is there. I know I'm overlooking where to plug it into but I can't seem to figure out what I'm missing. I haven't written the part for the price to be display in yet and with this being due in a few hours, I'm more worried to get it partially working, as opposed to not at all.

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionListener;


public class PizzaPricerFrame extends JFrame
{
	private static final int FRAME_WIDTH = 600;
    private static final int FRAME_HEIGHT = 700;
    
	private ActionListener listener;
	private JLabel nameLabel;
	private JTextField nameField;
	private JLabel savedNameLabel;
	private JButton button;
	private JPanel namePanel;
	private JComboBox sizeCombo;
	private JCheckBox thinCheckBox;
	private JCheckBox regularCheckBox;
	private JCheckBox thickCheckBox;
	private JRadioButton cheeseButton;
	private JRadioButton sauceButton;
	private JList toppingsList;
	private Container container;
	private final String[] toppings= {"Extra Sauce", "Extra Cheese", "Pepperoni", "Sausage", "Ham", "Pineapple", "Green Peppers", "Olives", "Spinich", "Mushrooms"};
	
	//construct the frame
	public PizzaPricerFrame()
	{
		savedNameLabel = new JLabel("Hi " + nameField);
		
		//helper methods
		createTextField();
		createButton();
		createPanel();
		createList();
		createRadioButtons();
		createComboBox();

		setSize(FRAME_WIDTH, FRAME_HEIGHT);	
	}

	//create the control panel for all the components
	public void createControlPanel()
	{
		JPanel namePanel = createPanel();
		JPanel sizePanel = createComboBox();
		JPanel crustGroupPanel = createRadioButtons();
		JPanel mainToppingsPanel = createCheckBoxes();
		JPanel toppingsPanel = createList();
		JPanel orderPanel = createTextArea();
		
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(6, 1));
		controlPanel.add(namePanel);
		controlPanel.add(sizePanel);
		controlPanel.add(crustGroupPanel);
		controlPanel.add(mainToppingsPanel);
		controlPanel.add(toppingsPanel);
		controlPanel.add(orderPanel);
		
		//add panels to content panel
		add(controlPanel);
		
	}
	private JPanel createPanel()
	{
		namePanel = new JPanel();
		namePanel.add(nameLabel);
		namePanel.add(nameField);
		namePanel.add(button);
		namePanel.add(savedNameLabel);
		add(namePanel);
		return namePanel;
	}

		//create text field ...
	private void createTextField()
	{
		nameLabel = new JLabel("Name: ");
		final int FIELD_WIDTH = 15;
		nameField = new JTextField(FIELD_WIDTH);
		nameField.setText(" ");
	}
	
	//create button for text field to save users name
	private void createButton()
	{
		button = new JButton("Save");
		
		class SaveNameListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{	savedNameLabel.setText(getName());	}
		}
		
		ActionListener listener = new SaveNameListener();
		button.addActionListener(listener);
	}

	//create the combo box to select size
	//@return the panel containing the combo box
	public JPanel createComboBox()
	{
		sizeCombo = new JComboBox();
		sizeCombo.addItem("Small: $5.00");
		sizeCombo.addItem("Medium: $6.00");
		sizeCombo.addItem("Large: $7.00");
		sizeCombo.setEditable(true);
		sizeCombo.addActionListener(listener);
		
		JPanel panel = new JPanel();
		panel.add(sizeCombo);
		return panel;
	}
	
	//create the radio buttons to select type of crust
	//@return the panel containing the radio buttons
	public JPanel createRadioButtons()
	{
		thinCheckBox = new JCheckBox("Thin Crust: $1.00");
		thinCheckBox.addActionListener(listener);
		
		regularCheckBox = new JCheckBox("Regular Crust ");
		regularCheckBox.addActionListener(listener);
		
		thickCheckBox = new JCheckBox("Thick Crust: $2.00");
		thickCheckBox.addActionListener(listener);
		
		//add radio buttons to button group
		JPanel panel = new JPanel();
		panel.add(thinCheckBox);
		panel.add(regularCheckBox);
		panel.add(thickCheckBox);
		panel.setBorder(new TitledBorder(new EtchedBorder(), "Crust"));
		return panel;
	}
	
	//create the check boxes for selecting basic toppings
	//@return the panel containing the radio buttons
	public JPanel createCheckBoxes()
	{
		sauceButton = new JRadioButton("Sauce");
		sauceButton.addActionListener(listener);
		
		cheeseButton = new JRadioButton("Cheese");
		cheeseButton.addActionListener(listener);
		
		//add radio buttons to button group
		
		ButtonGroup group = new ButtonGroup();
		group.add(sauceButton);
		group.add(cheeseButton);
		
		JPanel panel = new JPanel();
		panel.add(sauceButton);
		panel.add(cheeseButton);
		panel.setBorder(new TitledBorder(new EtchedBorder(), "Main Toppings"));
		
		return panel;
	}
	
	//create a list of other toppings
	//@return the panel containing the add-on's for the oder
	public JPanel createList()
	{
		container = getContentPane();
		container.setLayout( new FlowLayout() );
		toppingsList = new JList(toppings);
		toppingsList.setVisibleRowCount( 5 );
		toppingsList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		
		container.add(new JScrollPane(toppingsList));
		toppingsList.addListSelectionListener((ListSelectionListener) listener);
		
		setVisible(true);
		
		JPanel panel = new JPanel();
		panel.add(container);
		panel.setBorder(new TitledBorder(new EtchedBorder(), "Toppings"));

		return panel;
	}
	
	//create the text field that displays all of the users input
	 public JPanel createTextArea()
	 {
		 //get size
		 String size = (String) sizeCombo.getSelectedItem();
		 /**
		 if (sizeCombo("Small: $5.00").isSelected())
			 size = 5;
		 else if(sizeCombo(medium).isSelected())
		 	size = 6;
		 else if (sizeCombo(large).isSelected())
			 	size = 7;
		**/
		 
		 //get crust type
		 String crust;
		 int crustValue = 0;
		 if(thinCheckBox.isSelected())
			 crustValue = 1;
		 	crust = "thin Crust";
		 if (regularCheckBox.isSelected())
				 crustValue = 0;
		 			crust = "regular Crust";
		 if(thickCheckBox.isSelected())
			 crustValue = 2;
		 	crust = "thick Crust";
		 	
		 //get basic toppings
		 	String sauce;
		 	String cheese;
		 	
		 	if(sauceButton.isSelected())
		 		sauce = "with sauce";
		 	else
		 		sauce = "with no sauce";
		 	if(cheeseButton.isSelected())
		 		cheese = "and cheese";
		 	else
		 		cheese = "and no cheese";
		 	
		 	String basic = sauce + cheese;
		 	
		 	//get other toppings
		 	Object[] toppings = toppingsList.getSelectedValues();
		
		 	String order = " " + size + " " + crust +  " with " + basic + " and " + toppings + ".";
		 	
		 	JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(new EtchedBorder(), "Your order"));
		 
			return panel;
	 }
}



Here's my main

import javax.swing.JFrame;
public class PizzaPricerViewer {
	public static void main(String[] args) 
	{  
	      PizzaPricerFrame frame = new PizzaPricerFrame ();
	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      frame.setVisible(true);
	   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: JPanel isn't displaying what I need it to

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: JPanel isn't displaying what I need it to

Posted 07 December 2012 - 08:14 AM

To start you only need to import javax.swing.*;

That will import all functions in the swing package.
Was This Post Helpful? 0
  • +
  • -

#3 String lostInClass  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 12

Re: JPanel isn't displaying what I need it to

Posted 07 December 2012 - 11:35 AM

Okay, but that did nothing for making the program run correctly.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: JPanel isn't displaying what I need it to

Posted 07 December 2012 - 11:57 AM

View PostFlukeshot, on 07 December 2012 - 11:14 AM, said:

To start you only need to import javax.swing.*;

That will import all functions in the swing package.

Question of choice. Both approachs have pros and cons.

As the initial question is concerned oupf not easy to follow.. as all your methods retun a JPanel why don't you take the JPanel returned and add them to the JFrame in a centralized place

We have to search to find the container.setLayout(FlowLayout()) in a method named createList not easy to find where what is done and in which order.

Re-organize your code first

Still in createList() you add the container (the contentPane of the JFrame) into a JPanel

        JPanel panel = new JPanel();
	panel.add(container);


JPanel that yuou return panel; but never used anywhere
And what is the idea of putting the JFrame contentPane in a JPanel ?

ublic JPanel createComboBox()
also creates a JComboBox put into a JPanel that is returned but never add somewhere
Was This Post Helpful? 0
  • +
  • -

#5 String lostInClass  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 12

Re: JPanel isn't displaying what I need it to

Posted 08 December 2012 - 09:07 PM

thanks for the help pbl. I wasn't able to complete the assignment in time but I was at least able to get some of the content to appear in the JFrame from having worked on the tips you gave me. The partial credit I'll get will be better than none at all. Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1