Jump to content

Can't get JList to show in Panel

- - - - -

  • Please log in to reply
2 replies to this topic

#1
N u l l i f y

N u l l i f y

    Newbie

  • Members
  • Pip
  • 6 posts
Well, I'm trying to get my final project finished for my Programming 1 course, but I seem to be having trouble getting my JList to show up in its panel. The panel itself shows, but the JList doesn't. Any help would be greatly appreciated. Code is below...

PizzaListPanel

package finalproject;


import javax.swing.*;


/**

   This class demonstrates the List Component.

*/


public class PizzaListPanel extends JPanel

{

   private JPanel pizzaPanel;         // To hold components

   private JList pizzaList;           // The pizzas

   private JScrollPane scrollPane;    // A scroll pane


   // The following array holds the values that will

   // be displayed in the pizzaList component.

   private String[] pizzas = { "Create Your Own", "King Arthur Supreme",

            "Wombo Combo", "Maui Zaui", "Chicken Garlic Gourmet", "Guinivere's Garden Delight", "Gourmet Veggie",

            "Montague's All-Meat Marvel", "Ulti-Meat", "Italian Garlic Supreme", "Smokehouse Combo",

            "Chicken Smokehouse Combo", "BBQ Chicken", "Hawaiian" };

   //Constructor

   public PizzaListPanel()

   {


      // Create a panel to hold the list.

      pizzaPanel = new JPanel();


      // Create the list.

      pizzaList = new JList(pizzas);


      // Set the selection mode to single selection.

      pizzaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


      // Set the number of visible rows to 12.

      pizzaList.setVisibleRowCount(12);


      // Add the list to a scroll pane.

      scrollPane = new JScrollPane(pizzaList);


      // Add the scroll pane to the panel.

      pizzaPanel.add(scrollPane);


      // Add a border around the panel.

      setBorder(BorderFactory.createTitledBorder("Pizza"));

   }

}


PriceCalculatorGUI

package finalproject;


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.text.DecimalFormat;


/**

   The OrderCalculatorGUI class creates the GUI for the

   Brandi's Bagel House application.

*/


public class PriceCalculatorGUI extends JFrame

{

   private PizzaListPanel pizza;     // Bagel panel

   private PizzaSizePanel size; // Topping panel

   private ToppingPanel toppings; // Topping panel

   private OrderTypePanel type;    // Coffee panel

   private GreetingPanel banner;  // To display a greeting

   private JPanel buttonPanel;    // To hold the buttons

   private JButton calcButton;    // To calculate the cost

   private JButton exitButton;    // To exit the application

   private final double TAX_RATE = 0.0825; // Sales tax rate



   /**

      Constructor

   */


   public PriceCalculatorGUI()

   {

      // Display a title.

      setTitle("Price Calculator");


      // Specify an action for the close button.

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      // Create a BorderLayout manager.

      setLayout(new BorderLayout());


      // Create the custom panels.

      banner = new GreetingPanel();

      size = new PizzaSizePanel();

      pizza = new PizzaListPanel();

      toppings = new ToppingPanel();

      type = new OrderTypePanel();


      // Create the button panel.

      buildButtonPanel();


      // Add the components to the content pane.

      add(type, BorderLayout.NORTH);

      add(pizza, BorderLayout.CENTER);

      add(size, BorderLayout.WEST);

      add(toppings, BorderLayout.EAST);

      add(buttonPanel, BorderLayout.SOUTH);


      // Pack the contents of the window and display it.

      pack();

      setVisible(true);

   }


   /**

      The buildButtonPanel method builds the button panel.

   */


   private void buildButtonPanel()

   {

      // Create a panel for the buttons.

      buttonPanel = new JPanel();


      // Create the buttons.

      calcButton = new JButton("Calculate");

      exitButton = new JButton("Exit");


      // Register the action listeners.

      calcButton.addActionListener(new CalcButtonListener());

      exitButton.addActionListener(new ExitButtonListener());


      // Add the buttons to the button panel.

      buttonPanel.add(calcButton);

      buttonPanel.add(exitButton);

   }


   /**

      Private inner class that handles the event when

      the user clicks the Calculate button.

   */


   private class CalcButtonListener implements ActionListener

   {

      public void actionPerformed(ActionEvent e)

      {

         // Variables to hold the subtotal, tax, and total

         double subtotal, tax, total;


         // Calculate the subtotal.

         subtotal = type.getOrderTypeCost() +

                    toppings.getToppingCost()-(1*(size.getSizeToppingCost())) +

                    size.getSizeCost();


         // Calculate the sales tax.

         tax = subtotal * TAX_RATE;


         // Calculate the total.

         total = subtotal + tax;


         // Create a DecimalFormat object to format output.

         DecimalFormat dollar = new DecimalFormat("0.00");


         // Display the charges.

         JOptionPane.showMessageDialog(null, "Subtotal: $" +

                       dollar.format(subtotal) + "\n" +

                       "Tax: $" + dollar.format(tax) + "\n" +

                       "Total: $" + dollar.format(total));

      }

   }


   /**

      Private inner class that handles the event when

      the user clicks the Exit button.

   */


   private class ExitButtonListener implements ActionListener

   {

      public void actionPerformed(ActionEvent e)

      {

          System.exit(0);

      }

   }

}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
public class PizzaListPanel extends JPanel

{

   [B][SIZE="5"][COLOR="red"]private JPanel pizzaPanel;         // To hold components[/COLOR][/SIZE][/B]

   private JList pizzaList;           // The pizzas

   private JScrollPane scrollPane;    // A scroll pane


   // The following array holds the values that will

   // be displayed in the pizzaList component.

   private String[] pizzas = { "Create Your Own", "King Arthur Supreme",

            "Wombo Combo", "Maui Zaui", "Chicken Garlic Gourmet", "Guinivere's Garden Delight", "Gourmet Veggie",

            "Montague's All-Meat Marvel", "Ulti-Meat", "Italian Garlic Supreme", "Smokehouse Combo",

            "Chicken Smokehouse Combo", "BBQ Chicken", "Hawaiian" };

   //Constructor

   public PizzaListPanel()

   {


      // Create a panel to hold the list.

      pizzaPanel = new JPanel();


      // Create the list.

      pizzaList = new JList(pizzas);


      // Set the selection mode to single selection.

      pizzaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


      // Set the number of visible rows to 12.

      pizzaList.setVisibleRowCount(12);


      // Add the list to a scroll pane.

      scrollPane = new JScrollPane(pizzaList);


      // Add the scroll pane to the panel.

      pizzaPanel.add(scrollPane);


      // Add a border around the panel.

      setBorder(BorderFactory.createTitledBorder("Pizza"));

   }

}
There is the problem. Whatever you do in the constructor with pizzaPanel, do it with the class itself (or super).:c-smile:
End result:

import javax.swing.*;


/**

   This class demonstrates the List Component.

*/


public class PizzaListPanel extends JPanel

{

   private JList pizzaList;        

   private JScrollPane scrollPane;  

   private String[] pizzas = { "Create Your Own", "King Arthur Supreme",

            "Wombo Combo", "Maui Zaui", "Chicken Garlic Gourmet", "Guinivere's Garden Delight", "Gourmet Veggie",

            "Montague's All-Meat Marvel", "Ulti-Meat", "Italian Garlic Supreme", "Smokehouse Combo",

            "Chicken Smokehouse Combo", "BBQ Chicken", "Hawaiian" };


   public PizzaListPanel()

   {

      pizzaList = new JList(pizzas);

      pizzaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

      pizzaList.setVisibleRowCount(12);

      scrollPane = new JScrollPane(pizzaList);

      add(scrollPane);

      setBorder(BorderFactory.createTitledBorder("Pizza"));

   }

}



#3
N u l l i f y

N u l l i f y

    Newbie

  • Members
  • Pip
  • 6 posts
YAY! It's working!

Thanks for the assistance! :c-grin:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users