Results 1 to 4 of 4

Thread: comboboxes

  1. #1
    duncan913 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default comboboxes

    i need to be able to populate a second combo box based up the selection of the first. Im building a pseudo pizza shop for a project and if specialty pizza is selected for example, then the specialty pizza combo box will appear. thanks!

    this is what i have so far, and i have a separate class file that implements the panel.

    ************************************************** ********

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

    public class PizzaGuiPanel extends JPanel

    {

    final static NumberFormat currency = NumberFormat.getCurrencyInstance();

    private JLabel typeLabel;
    private JComboBox typeComboBox;
    private JLabel sizeLabel;
    private JComboBox sizeComboBox;
    private JLabel crustLabel;
    private JComboBox crustComboBox;
    private JLabel toppingCountLabel;
    private JComboBox toppingCountComboBox;
    private JLabel toppingsLabel;
    private JComboBox toppingsComboBox;
    private JLabel specialtyLabel;
    private JComboBox specialtyComboBox;
    private JLabel wingLabel;
    private JComboBox wingComboBox;
    private JLabel wingCountLabel;
    private JComboBox wingCountComboBox;
    private JLabel wingSauceLabel;
    private JComboBox wingSauceComboBox;
    private JLabel deliveryLabel;
    private JComboBox deliveryComboBox;
    private JTextField calculateTotalTextField;
    private JButton calculateTotalButton;
    private JLabel calculateTotalLabel;
    private static final String[] TYPE = {"Cheese Only", "Custom Toppings", "Specialty"};
    private static final String[] SIZE = {"Small", "Medium", "Large"};
    private static final String[] CRUST = {"Original", "Thin Crust", "Deep Dish"};
    private static final String[] TOPPINGCOUNT = {"1","2","3","4","5","6","7","8","9"};
    private static final String[] TOPPINGS = {"Mushrooms","Extra Cheese","Extra Sauce","Pineapple","Bell Pepper","Bacon","Pepperoni","Ham","Sausage"};
    private static final String[] SPECIALTY = {"'The Works' is topped with all available toppings.",
    "'Super Hawaiian' is a Ham, Bacon, Pineapple and Extra Cheese topped pizza with a special spice blend",
    "'4 Meat' is a Ham, Bacon, Sausage, and Pepperoni topped pizza with a special spice blend."};
    private static final String[] WING = {"Yes","No"};
    private static final String[] WINGCOUNT = {"6","12","18","24"};
    private static final String[] WINGSAUCE = {"Naked","Hot","Medium","Mild","BBQ","Honey BBQ","Teriyaki","Thai","Stupid Hot (the name says it all)"};




    public PizzaGuiPanel()
    {


    //create panel components
    typeLabel = new JLabel ("Please select what kind of pizza you would like: " );
    typeComboBox = new JComboBox(TYPE);
    typeComboBox.setSelectedIndex(2);


    sizeLabel = new JLabel ("Please select what Size of pizza you would like: " );
    sizeComboBox = new JComboBox(SIZE);

    crustLabel = new JLabel ("What kind of crust would you like? " );
    crustComboBox = new JComboBox(CRUST);

    toppingCountLabel = new JLabel ("How Many Toppings Would You Like? " );
    toppingCountComboBox = new JComboBox(TOPPINGCOUNT);

    toppingsLabel = new JLabel ("Toppings: " );
    toppingsComboBox = new JComboBox(TOPPINGS);

    specialtyLabel = new JLabel ("Specialty Pizza's: " );
    specialtyComboBox = new JComboBox(SPECIALTY);

    wingLabel = new JLabel ("Would you like to Add Wings to your order? " );
    wingComboBox = new JComboBox(WING);

    wingCountLabel = new JLabel ("How Many Wings Would You like? " );
    wingCountComboBox = new JComboBox(WINGCOUNT);

    wingSauceLabel = new JLabel ("What Kind Of Sauce Would you Like? " );
    wingSauceComboBox = new JComboBox(WINGSAUCE);

    calculateTotalTextField = new JTextField (10);
    calculateTotalLabel = new JLabel("Calculate Total");
    calculateTotalButton = new JButton("Calulate Total");






    //add components to panel
    this.add(typeLabel);
    this.add(typeComboBox);
    this.add(sizeLabel);
    this.add(sizeComboBox);
    this.add(crustLabel);
    this.add(crustComboBox);
    this.add(toppingCountLabel);
    this.add(toppingCountComboBox);
    this.add(toppingsLabel);
    this.add(toppingsComboBox);
    this.add(specialtyLabel);
    this.add(specialtyComboBox);
    this.add(wingLabel);
    this.add(wingComboBox);
    this.add(wingCountLabel);
    this.add(wingCountComboBox);
    this.add(wingSauceLabel);
    this.add(wingSauceComboBox);
    this.add(calculateTotalButton);
    this.add(calculateTotalTextField);

    this.setPreferredSize(new Dimension(1200,300));




    }
    private class ComboBoxListener implements ActionListener
    {


    public void actionPerformed (ActionEvent event)

    {
    }
    }




    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Please use the code tags: the idea is that you put [CODE] at the start of your code and [/CODE]. The forum will then preserve your formatting so that the code's readable.

    if specialty pizza is selected for example, then the specialty pizza combo box will appear

    If I wanted one of a predefined number of combo boxes to appear depending on a selection made in another widget (ie type box selection --> appropriate label + combobox) I would place all of the label+combobox pairs into another panel with a CardLayout. The type combobox would be given an action listener whose job it was to set the currently displayed "card".

    That way only one subselection at a time would be visible, but all would exist and be available to form part of the calculation.

  3. #3
    duncan913 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the feedback, i used that but cant figure out how to take it to another level now, so if they choose cheese, then size is populated, but then once size is chosen i want crust type to appear.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    but then once size is chosen i want crust type to appear.

    Again the action listener added to the widget where size is set would set the card panel's "card" to the panel holding the crust type widget.

    What is emerging here is a sort of "wizard" dialog: you might want to google for how people do this with Java.

    If there are specific problems you will get specific advice if you post code. It need not be - and, indeed, should not be - a dump of your existing code. But rather something that compiles, is runnable and shows the approach you are taking with as few panels and comboboxes as is needed to show that approach.

Similar Threads

  1. Replies: 3
    Last Post: 06-02-2010, 07:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •