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

    Default Help with adding to an array in another class?

    I need to add from what is selected in some check boxes to an array that is in another class, but I can't get the other class to be called correctly. Any insight?

    I need to get the toppings that are selected from the check boxes added into the array that is in the calculations method. Any help is appreciated.

    Calculations method
    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 PizzaOrder
    {
       private int inch;
       private String crustType;
       private ArrayList<String> toppings;
       private String name; 
       private int price;
    
       public PizzaOrder (int size, String crustType, String name)
       {
          
          
       }
       
       public void addTopping (String topping)
       {
          toppings = new ArrayList<String>();
       }
       
       private double calculatePrice()
       {
          
          return price;
       }
       
       public String toString()
       {
          return s;
       }  
    }
    GUI method
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class PizzaOrderListener
    {
       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);
          
          JCheckBox pepperoni = new JCheckBox("Pepperoni");
          JCheckBox sausage = new JCheckBox("Sausage");
          JCheckBox bacon = new JCheckBox("Bacon");
          JCheckBox mushroom = new JCheckBox("Mushroom");
          JCheckBox cheesey = new JCheckBox("Extra Cheese");
          panel.add(pepperoni);
          panel.add(sausage);
          panel.add(bacon);
          panel.add(mushroom);
          panel.add(cheesey);
          
          
          //Button to submit.    
          JButton button = new JButton("Submit");
          panel.add(button);
          JPanel southPanel = new JPanel();
          final JLabel label = new JLabel("Please create your pizza.");
          southPanel.add(label);
          frame.add(label, BorderLayout.SOUTH);
          
          //Button to clear.
          JButton button2 = new JButton("Clear");
          panel.add(button2);
                
          PizzaOrder pizza = new PizzaOrder();
          
                
          class ClickListener implements ActionListener 
          {
             public void actionPerformed(ActionEvent event)   
             {  
                if(pepperoni.isSelected());
                   pizza.addTopping("Pepperoni");
                   
                if(sausage.isSelected());
                   pizza.addTopping("Pepperoni");
                   
                if(bacon.isSelected());
                   pizza.addTopping("Pepperoni");
                   
                if(mushroom.isSelected());
                   pizza.addTopping("Pepperoni");
                   
                if(cheesey.isSelected());
                   pizza.addTopping("Pepperoni");
               
             }            
          }      
          ActionListener listener = new ClickListener();
          button.addActionListener(listener);
          frame.setSize(400, 200);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);  
       }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Help with adding to an array in another class?

    This makes no sense:
    Java Code:
     public void addTopping (String topping)
       {
          toppings = new ArrayList<String>();
       }
    What you're saying here is that every time you add a topping, you want to set the toppings ArrayList (not array) to an empty ArrayList. Can you explain, in English, what you're trying to do here?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    Default Re: Help with adding to an array in another class?

    Quote Originally Posted by KevinWorkman View Post
    This makes no sense:
    Java Code:
     public void addTopping (String topping)
       {
          toppings = new ArrayList<String>();
       }
    What you're saying here is that every time you add a topping, you want to set the toppings ArrayList (not array) to an empty ArrayList. Can you explain, in English, what you're trying to do here?
    Umm I need to take what is selected from the check boxes and add them to that array.

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

    Default Re: Help with adding to an array in another class?

    Isn't this the same as this?
    http://www.java-forums.org/new-java/...heckboxes.html

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

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Help with adding to an array in another class?

    Quote Originally Posted by jim829 View Post
    You can tell the semester is ending and people are working on their final projects...
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  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 adding to an array in another class?

    Yeah. But it's the same OP. Different title but similar question.

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

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default Re: Help with adding to an array in another class?

    Quote Originally Posted by jim829 View Post
    Yeah. But it's the same OP. Different title but similar question.
    Yeah I know. I just meant the frantic scrambling that results in crossposts, etc.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

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

    Default Re: Help with adding to an array in another class?

    Quote Originally Posted by SmokeyWoody View Post
    Umm I need to take what is selected from the check boxes and add them to that array.
    Yeah... I forgot about that post... Woops. :P

Similar Threads

  1. adding an object to an array in another class
    By jwood in forum New To Java
    Replies: 92
    Last Post: 08-02-2013, 09:59 PM
  2. help adding to array from another class
    By Grid_iso in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 10:00 AM
  3. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  4. Array always adding to 0???
    By Javawizard in forum New To Java
    Replies: 6
    Last Post: 11-13-2010, 11:02 PM
  5. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 05:41 AM

Posting Permissions

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