Results 1 to 6 of 6
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Help with arraylist connected to checkboxes

    So I have to create an arraylist that is connected to check boxes. Here are my current codes that I am working with. I am just focusing on making the GUI before I get into making the price calculator and everything along with that.

    I need to apply the check boxes that are in the listener to the arraylist that is in the original class
    Java Code:
          PizzOrderPA3 toppings2 = new PizzaOrderPA3
          
          JCheckBox toppings2 = new JCheckBox();
          toppings2.addTopping("Pepperoni");
          toppings2.add("Sausage");
          toppings2.add("Mushroom");
          toppings2.add("Bacon");
          toppings2.add("Extra cheese");
    That is the code that I am working with but I have no idea with how to go along with this. So if someone could hint me in the right direction, I would be more then grateful.

    Java Code:
    import java.util.ArrayList;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JMenu; 
    import javax.swing.JMenuBar; 
    import javax.swing.JMenuItem;
    
    public class PizzaOrderPA3
    {
       private int inch;
       private String crustType;
       private ArrayList<String> toppings;
       private String name; 
       private int price;
    
       public PizzaOrderPA3 (int size, String crustType, String name)
       {
          
          
       }
       
       public void addTopping (String topping)
       {
          toppings = new ArrayList<String>();
       }
       
       private double calculatePrice()
       {
          
          return price;
       }
       
       public String toString()
       {
       
       }  
    }
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class PizzaOrderPA3Listener
    {
       public static void main(String[] args)        
       {  
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          frame.add(panel); 
          panel.setBackground(Color.WHITE);
          
          JTextField nameInput;
          nameInput = new JTextField(30);
          panel.add(nameInput);
          
          JComboBox size = new JComboBox();
          size.addItem("Select a size");
          size.addItem("10 Inch pizza - $10.79");
          size.addItem("12 Inch pizza - $12.79");
          size.addItem("14 Inch pizza - $14.79");
          size.addItem("16 inch pizza - $16.79");
          panel.add(size);
          
          JComboBox crust = new JComboBox();
          crust.addItem("Select a crust");
          crust.addItem("Hand tossed");
          crust.addItem("Deep dish");
          crust.addItem("Thin Crust");
          panel.add(crust);
          
          PizzOrderPA3 toppings2 = new PizzaOrderPA3
          
          JCheckBox toppings2 = new JCheckBox();
          toppings2.addTopping("Pepperoni");
          toppings2.add("Sausage");
          toppings2.add("Mushroom");
          toppings2.add("Bacon");
          toppings2.add("Extra cheese");
          
          frame.setSize(400, 200);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);  
       }
    }
    Last edited by SmokeyWoody; 11-28-2014 at 08:29 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help with arraylist connected to checkboxes

    Do you mean a JCombBox? (not a JCheckBox)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Re: Help with arraylist connected to checkboxes

    Quote Originally Posted by JosAH View Post
    Do you mean a JCombBox? (not a JCheckBox)

    kind regards,

    Jos
    Nope, I do in fact mean check boxes. Says so right on the assignment sheet.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Help with arraylist connected to checkboxes

    Don't you need mutliple check boxes then?
    One for each topping?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help with arraylist connected to checkboxes

    You can't add multiple toppings (Strings) to a single JCheckBox (see lines #4 ... #8); the code compiles (accidentally) because a JCheckox extends a Container, but it doesn't do what you want it to do.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with arraylist connected to checkboxes

    From the first snippet of code:

    Java Code:
    PizzOrderPA3 toppings2 = new PizzaOrderPA3
     
    JCheckBox toppings2 = new JCheckBox();
    toppings2.addTopping("Pepperoni");
    toppings2.add("Sausage");
    Your first line is incomplete and you have redefined toppings2. And addTopping is certainly not a method in JCheckBox. In general, I think you need multiple check boxes as Tolls suggested (one for each topping).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. How to add Checkboxes to a JTree
    By Reptar693 in forum AWT / Swing
    Replies: 0
    Last Post: 04-03-2012, 07:51 AM
  2. Randomize checkboxes
    By ZeCute in forum AWT / Swing
    Replies: 0
    Last Post: 05-11-2011, 09:19 AM
  3. ComboBox with CheckBoxes
    By heba.farouk in forum AWT / Swing
    Replies: 14
    Last Post: 06-09-2010, 12:03 PM
  4. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 09:01 PM
  5. How to use Mnemonic for CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:45 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
  •