Need help with an application using tabbed panes and collection components

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meekstro
    New Member
    • Sep 2008
    • 6

    Need help with an application using tabbed panes and collection components

    I have been assigned to design an application that uses tabbed panes for different catagories and collects information bout size and quantity. Also it is suppose to display the cost of the order as information is gathered. How should I go about displaying the cost? I have the tabbed panes set up. I was thinking I need to utilize some sort of actionlistener but am not sure how to implement this. Thanks for the help.

    I have included some of my code so far.
    [code=java]
    import javax.swing.*;

    public class Layout
    {
    public static void main (String[] args)
    {
    JFrame frame = new JFrame ("Placing Orders");
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

    JTabbedPane tp = new JTabbedPane();
    tp.addTab ("Intro", new IntroPanel());
    tp.addTab ("Pizza Selection", new PizzaSelectionP anel());
    tp.addTab ("Beverage Selection", new BeverageSelecti onPanel());
    tp.addTab ("Special Items", new SpecialSelectio nPanel());


    frame.getConten tPane().add(tp) ;
    frame.pack();
    frame.setVisibl e(true);
    }
    }


    import java.awt.*;
    import javax.swing.*;

    public class IntroPanel extends JPanel
    {
    public IntroPanel()
    {
    setBackground (Color.red);

    JLabel I1 = new JLabel ("Placing Orders");
    JLabel I2 = new JLabel ("Choose a tab to see ordering options.");

    add (I1);
    add (I2);
    }
    }


    import java.awt.*;
    import javax.swing.*;

    public class PizzaSelectionP anel extends JPanel
    {

    private JRadioButton small, medium, large;
    private JRadioButton one, two, three;
    public PizzaSelectionP anel()
    {

    setLayout (new FlowLayout());
    setBackground (Color.white);


    small = new JRadioButton ("Small");
    medium = new JRadioButton ("Medium");
    large = new JRadioButton ("Large");


    ButtonGroup group = new ButtonGroup();
    group.add (small);
    group.add (medium);
    group.add (large);


    add (small);
    add (medium);
    add (large);

    one = new JRadioButton ("One");
    two = new JRadioButton ("Two");
    three = new JRadioButton ("Three");

    ButtonGroup group2 = new ButtonGroup();
    group2.add (one);
    group2.add (two);
    group2.add (three);

    add (one);
    add (two);
    add (three);




    }
    }[/code]
    Last edited by Nepomuk; Nov 19 '08, 04:19 AM. Reason: Please use [CODE] tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Are the quantities limited to only 1, 2 and 3 ?
    2.) Where is the total cost going to be displayed?
    Create an action by extending the AbstractAction class. The action should do the calculation of the total based on what is selected and will perhaps be updating some "global" variable somewhere.

    Comment

    • meekstro
      New Member
      • Sep 2008
      • 6

      #3
      Originally posted by r035198x
      1.) Are the quantities limited to only 1, 2 and 3 ?
      2.) Where is the total cost going to be displayed?
      Create an action by extending the AbstractAction class. The action should do the calculation of the total based on what is selected and will perhaps be updating some "global" variable somewhere.

      I just limited the quantity to three. The assignment does not specify either way. I was thinking to display the cost I would have to include another tabbed pane used specifically to display cost. I am pretty certain I have to implement some ActionListener' s to count the the action events created by the radio button, but am having a little trouble figuring out how to assign values to each size selection and getting the listener's to keep track of the action selected.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        The best place to start is to read Sun's swing tutorial.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          I don't even know what a 'collection component' is ...

          kind regards,

          Jos

          Comment

          • meekstro
            New Member
            • Sep 2008
            • 6

            #6
            Here is what I have come up with. Basically all I have left is too call this information in another tabbed pane so that as the user picks the size and quantity of each selection a total cost is tallied. How do I go about calling each selection to be totaled up in a completely different pane?



            import javax.swing.*;

            public class Layout
            {
            public static void main (String[] args)
            {
            JFrame frame = new JFrame ("Placing Orders");
            frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

            JTabbedPane tp = new JTabbedPane();
            tp.addTab ("Intro", new IntroPanel());
            tp.addTab ("Pizza Selection", new PizzaSelectionP anel());
            tp.addTab ("Beverage Selection", new BeverageSelecti onPanel());
            tp.addTab ("Special Items", new SpecialSelectio nPanel());
            tp.addTab ("Total Cost", new TotalCostPanel( ));

            frame.getConten tPane().add(tp) ;
            frame.pack();
            frame.setVisibl e(true);
            }
            }


            import java.awt.*;
            import javax.swing.*;

            public class IntroPanel extends JPanel
            {
            public IntroPanel()
            {
            setBackground (Color.red);

            JLabel I1 = new JLabel ("Placing Orders");
            JLabel I2 = new JLabel ("Choose a tab to see ordering options.");

            add (I1);
            add (I2);
            }
            }

            import java.awt.*;
            import javax.swing.*;
            import java.awt.event. *;
            import javax.swing.JOp tionPane;


            public class PizzaSelectionP anel extends JPanel
            {
            int num1, num2, again;
            String numStr, numStr2, result;
            private JRadioButton small, medium, large;

            public PizzaSelectionP anel()
            {
            setLayout (new FlowLayout());
            setBackground (Color.yellow);

            small = new JRadioButton ("Small");
            medium = new JRadioButton ("Medium");
            large = new JRadioButton ("Large");

            ButtonGroup group = new ButtonGroup();
            group.add (small);
            group.add (medium);
            group.add (large);

            ButtonListener listener = new ButtonListener( );
            small.addAction Listener (listener);
            medium.addActio nListener (listener);
            large.addAction Listener (listener);

            add (small);
            add (medium);
            add (large);


            }

            private class ButtonListener implements ActionListener
            {
            public void actionPerformed (ActionEvent event)
            {

            do
            {
            int size = 0;
            if (small.isSelect ed())
            size = 5;
            else
            if (medium.isSelec ted())
            size = 12;
            else
            size = 15;


            numStr2 = JOptionPane.sho wInputDialog ("Enter a Quantity: ");
            num2 = Integer.parseIn t(numStr2);

            result = "That Cost is $ " + (num2*size);

            JOptionPane.sho wMessageDialog (null, result);

            again = JOptionPane.sho wConfirmDialog (null, "Is the Correct?");

            }
            while (again == JOptionPane.NO_ OPTION);


            }

            }
            }



            import java.awt.*;
            import javax.swing.*;
            import java.awt.event. *;
            import javax.swing.JOp tionPane;

            public class BeverageSelecti onPanel extends JPanel
            {

            private JRadioButton small, medium, large;
            int num2, again;
            String numStr2, result;

            public BeverageSelecti onPanel()
            {

            setLayout (new FlowLayout());
            setBackground (Color.blue);

            small = new JRadioButton ("Small");
            medium = new JRadioButton ("Medium");
            large = new JRadioButton ("Large");

            ButtonGroup group = new ButtonGroup();
            group.add (small);
            group.add (medium);
            group.add (large);

            ButtonListener listener = new ButtonListener( );
            small.addAction Listener (listener);
            medium.addActio nListener (listener);
            large.addAction Listener (listener);

            add (small);
            add (medium);
            add (large);
            }

            private class ButtonListener implements ActionListener
            {
            public void actionPerformed (ActionEvent event)
            {

            do
            {
            int size = 0;
            if (small.isSelect ed())
            size = 2;
            else
            if (medium.isSelec ted())
            size = 3;
            else
            size = 4;


            numStr2 = JOptionPane.sho wInputDialog ("Enter a Quantity: ");
            num2 = Integer.parseIn t(numStr2);

            result = "That Cost is $ " + (num2*size);

            JOptionPane.sho wMessageDialog (null, result);

            again = JOptionPane.sho wConfirmDialog (null, "Is the Correct?");

            }
            while (again == JOptionPane.NO_ OPTION);


            }

            }
            }

            import java.awt.event. *;
            import javax.swing.JOp tionPane;

            public class SpecialSelectio nPanel extends JPanel
            {

            private JCheckBox top1, top2, top3;

            public SpecialSelectio nPanel()
            {

            setLayout (new FlowLayout());
            setBackground (Color.green);

            top1 = new JCheckBox ("Pepperoni" );
            top2 = new JCheckBox ("Extra Cheese");
            top3 = new JCheckBox ("Sausage");

            ToppingListener listener = new ToppingListener ();
            top1.addItemLis tener (listener);
            top2.addItemLis tener (listener);
            top3.addItemLis tener (listener);


            add (top1);
            add (top2);
            add (top3);
            }

            private class ToppingListener implements ItemListener
            {

            public void itemStateChange d (ItemEvent event)
            {
            int cost = 0;

            if (top1.isSelecte d())
            cost = 1;
            else
            if (top2.isSelecte d())
            cost = 1;
            else
            cost = 2;
            }
            }
            }


            import java.awt.*;
            import javax.swing.*;
            import java.awt.event. *;
            import javax.swing.JOp tionPane;

            public class TotalCostPanel extends JPanel
            {

            int totalCost, PizzaSelectionP anel, BeverageSelecti onPanel, SpecialSelectio nPanel ;

            public TotalCostPanel( )
            {
            setLayout (new FlowLayout());
            setBackground (Color.orange);


            totalCost = PizzaSelectionP anel + BeverageSelecti onPanel + SpecialSelectio nPanel;

            ?????????
            }
            }

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by meekstro
              Code:
              totalCost = PizzaSelectionPanel + BeverageSelectionPanel + SpecialSelectionPanel;
              There you go: make each of those panels a little class and implement a method
              'getTotal()' in each one of them. Those panels should know how to get their sub-
              total. All you need to do is get all the totals and sum them.

              In OO programming a few little classes don't harm.

              kind regards,

              Jos

              Comment

              • meekstro
                New Member
                • Sep 2008
                • 6

                #8
                I think Im way off here but is this kinda what I'm needing to do?

                Thanks for your help.



                import java.awt.*;
                import javax.swing.*;
                import java.awt.event. *;
                import javax.swing.JOp tionPane;

                public class TotalCostPanel extends JPanel
                {
                int total, num2, size, cost;
                int totalCost, PizzaSelectionP anel, BeverageSelecti onPanel, SpecialSelectio nPanel ;

                public class PizzaSelectionP anel
                {
                public int getTotal()
                {
                total = num2*size;
                return total;
                }
                }

                public class BeverageSelecti onPanel
                {
                public int getTotal()
                {
                total = num2*size;
                return total;
                }
                }

                public class SpecialSelectio nPanel
                {
                public int getTotal()
                {
                total = cost;
                return total;
                }
                }


                public TotalCostPanel( )
                {
                setLayout (new FlowLayout());
                setBackground (Color.orange);

                totalCost = PizzaSelectionP anel + BeverageSelecti onPanel + SpecialSelectio nPanel;
                System.out.prin tln(totalCost);

                }
                }

                Comment

                Working...