Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,796 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,323 people online right now. Registration is fast and FREE... Join Now!




Layout question

 
Reply to this topicStart new topic

Layout question, pizza order form homework

imjuzaround
1 Oct, 2008 - 03:47 AM
Post #1

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
i have only one question....


what are the differences made by using flowlayout and set of bounce sort of layout.... i am customed using flowlayout....

juz wondering what's the best to use in making my pizza order form homework tongue.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Layout Question
1 Oct, 2008 - 03:51 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,135



Thanked: 203 times
Dream Kudos: 75
My Contributions
QUOTE(imjuzaround @ 1 Oct, 2008 - 04:47 AM) *

i have only one question....


what are the differences made by using flowlayout and set of bounce sort of layout.... i am customed using flowlayout....

juz wondering what's the best to use in making my pizza order form homework tongue.gif


Never heard of "set of bounce sort of layout". You mean null layout where you will set the bounds of every component ?

*Post title edited to be more desciptive

This post has been edited by pbl: 1 Oct, 2008 - 03:53 AM
User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
1 Oct, 2008 - 07:19 PM
Post #3

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
QUOTE(pbl @ 1 Oct, 2008 - 04:51 AM) *

QUOTE(imjuzaround @ 1 Oct, 2008 - 04:47 AM) *

i have only one question....


what are the differences made by using flowlayout and set of bounce sort of layout.... i am customed using flowlayout....

juz wondering what's the best to use in making my pizza order form homework tongue.gif


Never heard of "set of bounce sort of layout". You mean null layout where you will set the bounds of every component ?

*Post title edited to be more desciptive



ahhmm...sort of....im just a newbie in java...and i forgot the exact name of the layout tongue.gif

im still working on my code..i find it difficult using flowlayout so i am wondering how to use the other one... tongue.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Layout Question
1 Oct, 2008 - 07:29 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,135



Thanked: 203 times
Dream Kudos: 75
My Contributions
QUOTE(imjuzaround @ 1 Oct, 2008 - 08:19 PM) *

ahhmm...sort of....im just a newbie in java...and i forgot the exact name of the layout tongue.gif

im still working on my code..i find it difficult using flowlayout so i am wondering how to use the other one... tongue.gif


You will have problem to JPanel.setLayout(layoutName); if you don't know it's name...

If you use a null layout you will have to set the bounds (x, y, width, height) of every component you put into it

For pizza order I would use a gGridLayout(10, 2); 10 lines 2 columns
In the left column JLabels defining what is on the second column:

CODE

Pizza Size:          Large Medium Small  (RadioButton)
Cheeze:              CheckBox
Peepperoni          CheckBox
....
Sub-Total:           Price in JLabel
Tax:                    Price in JLabel
Total:                  Price in JLabel


User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
3 Oct, 2008 - 02:34 AM
Post #5

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
i started my code....this way

CODE

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


class pizza extends JFrame{
    public static void main (String[] args) {
     pizza frenzy=new pizza();
     frenzy.bebang();
    }
    
    JPanel a;
    JButton reset,add;
    JComboBox quantity;
    JRadioButton size,crust,dscnt;
    JCheckBox toppings;
    JTextField yes,tamnt,ramnt,change,show;
    
      public void bebang(){
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      
      setTitle("pizzaform");
      String qty[]={"1","2","3","4","5"};
      quantity=new JComboBox(qty);
      a.add(quantity);
      add(a);
    pack();
    setVisible(true);
}

}


but i encountered this error..while running it crazy.gif

Exception in thread "main" java.lang.NullPointerException
at pizza.bebang(pizza.java:25)
at pizza.main(pizza.java:9)


what is the problem with my code?sad.gif
User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
3 Oct, 2008 - 02:39 AM
Post #6

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
QUOTE(imjuzaround @ 3 Oct, 2008 - 03:34 AM) *

i started my code....this way

CODE

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


class pizza extends JFrame{
    public static void main (String[] args) {
     pizza frenzy=new pizza();
     frenzy.bebang();
    }
    
    JPanel a;
    JButton reset,add;
    JComboBox quantity;
    JRadioButton size,crust,dscnt;
    JCheckBox toppings;
    JTextField yes,tamnt,ramnt,change,show;
    
      public void bebang(){
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      
      setTitle("pizzaform");
      String qty[]={"1","2","3","4","5"};
      quantity=new JComboBox(qty);
      a.add(quantity);
      add(a);
    pack();
    setVisible(true);
}

}


but i encountered this error..while running it crazy.gif

Exception in thread "main" java.lang.NullPointerException
at pizza.bebang(pizza.java:25)
at pizza.main(pizza.java:9)


what is the problem with my code?sad.gif



weeebie..i figured out my problem...i just forgot to specify what layout in my panel:)
User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
3 Oct, 2008 - 02:55 AM
Post #7

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
could you please enhance my code??i got some difficulties making it because im just a newbie....sad.gif

i had tos et its size using radiobutton tehn checkbox for its toppings... another radio button for the crust, sad.gif



User is offlineProfile CardPM
+Quote Post

pbl
RE: Layout Question
3 Oct, 2008 - 04:28 AM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,135



Thanked: 203 times
Dream Kudos: 75
My Contributions
Here is a good start

java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


class pizza extends JFrame implements ActionListener{

public static void main (String[] args) {
pizza frenzy=new pizza();
frenzy.bebang();
}

JPanel panel;
JButton reset,add;
JComboBox quantity;
JRadioButton size,crust,dscnt;
String[] toppingList = {"Cheeze", "Green Peppers", "Mushroom", "Pepperoni"};
JButton order;
String qty[]={"1","2","3","4","5"};
JCheckBox[] toppings;
JTextField yes,tamnt,ramnt,change,show;

public void bebang(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("pizzaform");
setLayout(new BorderLayout());
// one line for each topping + 1 for quantity + 1 for total
panel = new JPanel(new GridLayout(toppingList.length + 1 + 1, 2));

toppings = new JCheckBox[toppingList.length];
// adding the topping
for(int i = 0; i < toppingList.length; i++) {
panel.add(new JLabel(toppingList[i]));
toppings[i] = new JCheckBox();
panel.add(toppings[i]);
}
quantity=new JComboBox(qty);
panel.add(new JLabel("Quantity"));
panel.add(quantity);
add(panel, BorderLayout.CENTER);

JButton order = new JButton("Order");
order.addActionListener(this);

add(order, BorderLayout.SOUTH);

setVisible(true);
setSize(200, 200);
}

public void actionPerformed(ActionEvent arg0) {
System.out.println("Order button has been pressed");
for(int i = 0; i < toppings.length; i++)
System.out.println(toppingList[i] + " is " + toppings[i].isSelected());
System.out.println("Quantity is " + (quantity.getSelectedIndex() + 1));
}

}



User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
4 Oct, 2008 - 12:08 AM
Post #9

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
woahh.... biggrin.gif tnx for helping me out on that one...


i have another question...my teacher told us to use group button for the size choices...i dunno how to declare that in java...the sizes were small, medium,large and family size... ohno.gif

im about to finnish this.... tnx in advance.... tongue.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Layout Question
4 Oct, 2008 - 08:08 AM
Post #10

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,135



Thanked: 203 times
Dream Kudos: 75
My Contributions
QUOTE(imjuzaround @ 4 Oct, 2008 - 01:08 AM) *

woahh.... biggrin.gif tnx for helping me out on that one...


i have another question...my teacher told us to use group button for the size choices...i dunno how to declare that in java...the sizes were small, medium,large and family size... ohno.gif

im about to finnish this.... tnx in advance.... tongue.gif


You will also have to put your RadioButton in their own panel so the N JRadio will be together

CODE

String[] sizeName = {"Small", "Medium", "Large");
JPanel radioPanel;
JRadioButton[] radio;
....
ButtonGroup group = new ButtonGroup();
radio = new JRadioButton[sizeName.length];
radioPanel = new JPanel(new GridLayout(1, radio.length));
for(int i = 0; i < radio.length; i++) {
   radio[i] = new JRadioButton(sizeName[i]);
   group.add(radio[i]);
   radioPanel.add(radio[i]);
}

// add another row to my main panel containing
// the label "size" on the left
// the 3 radio on the right
panel.add(new JLabel("Size:"));
panel.add(radioPanel);



User is offlineProfile CardPM
+Quote Post

imjuzaround
RE: Layout Question
13 Oct, 2008 - 02:50 AM
Post #11

New D.I.C Head
*

Joined: 8 Mar, 2008
Posts: 16


My Contributions
weeeebie.......i did my homework...tnx for the help...by the way...here's my code......


CODE


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

public class pizzafrenzy extends JFrame
{

        public JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8,panel9,panel10,panel11,p
anel12,panel13;;

        public JComboBox thecombo;
        public JRadioButton size,yes,no;
        public ButtonGroup pizzasize,crust,dscnt;
        public JRadioButton small,medium,large,famsize;
        public JRadioButton h,t,d;
        public JCheckBox sausage,pepperoni,cheese,ham,olive,mushroom,pineapple;
        public JLabel label1,label2,label3,toppings,c,discount1,discount2,discount3,totAMNT,tenamt,cha
nge;
        public JTextArea lapad;
        public JButton reset,add;
        public JTextField dcount,tot,ten,cha;
        public JScrollPane iskrol;
        public JLabel webeei,out;
        public int mambo;
        public    int num=0,number=0;
        public String istilo="",s="";
        public double subtotal=0.0;
        public double price=0.0;
        public double total=0.0;
            public double D=0.0;
    public int bebang[]=new int[100];
    
        
    public static void main(String[]args)
    {
        pizzafrenzy frenzy = new pizzafrenzy();
        frenzy.bear();


    }

        public double tsekbaksA=0,disc=0.0,says=0.0;
        double tsekbaks=0.0;double tseksbaksB=0.0;double tseksbaksC=0.0;double tseksbaksD=0.0;double tseksbaksE=0.0;double tseksbaksF=0.0;double tseksbaksG=0.0;
        public void bear()
        {
            setTitle("Pizza Order Form");
            setSize(340,600);
            setResizable(false);
            setLocationRelativeTo(null);
            setLayout(new GridLayout(0,1));

        
            panel1 = new JPanel(new FlowLayout());
            panel4 = new JPanel(new FlowLayout());
            
            panel5 = new JPanel(new GridLayout(4,2));
            panel6 = new JPanel(new BorderLayout());
            panel2 = new JPanel(new BorderLayout());
            
            
            panel11=new JPanel(new FlowLayout());
            panel12=new JPanel(new BorderLayout());
            
            panel7=new JPanel(new FlowLayout());
            panel8=new JPanel(new BorderLayout());
            
            panel13=new JPanel(new BorderLayout());
            
            panel9=new JPanel(new GridLayout(3,2));
            panel10=new JPanel(new BorderLayout());    


            
            label1 = new JLabel("Quantity");
            label3 = new JLabel("Box(es)                                                        ");
            
            thecombo = new JComboBox();

            for(int x=0;x<100;x++)
            {
                num++;
                bebang[x]=num;
                thecombo.addItem(Integer.toString(bebang[x]));
            }
            ComboHandler comHandler=new ComboHandler();
            thecombo.addItemListener(comHandler);
            
            
            RadioHandler rhandler=new RadioHandler();
            label2 = new JLabel("   Size");
            pizzasize = new ButtonGroup();
            
            small = new JRadioButton("Small");
            small.addActionListener(rhandler);
             medium= new JRadioButton("Medium");
            medium.addActionListener(rhandler);
            large = new JRadioButton("Large");
            large.addActionListener(rhandler);
            famsize = new JRadioButton("Family Size");
            famsize.addActionListener(rhandler);

            pizzasize.add(small);
            pizzasize.add(medium);
            pizzasize.add(large);
            pizzasize.add(famsize);

            panel1.add(label1);            
            panel1.add(thecombo);
            panel1.add(label3);
            panel1.add(label2);            
            panel1.add(small);
            panel1.add(medium);
            panel1.add(large);
            panel1.add(famsize);

            
            
            CheckboxHandler chandler=new CheckboxHandler();
            toppings = new JLabel("    Toppings        ");
            
            sausage = new JCheckBox("Sausage ");
            sausage.addItemListener(chandler);
            pepperoni = new JCheckBox("Pepperoni");
            pepperoni.addItemListener(chandler);
            cheese = new JCheckBox("Cheese");
            cheese.addItemListener(chandler);
            ham = new JCheckBox("Ham");
            ham.addItemListener(chandler);
            olive = new JCheckBox("Olive");
            olive.addItemListener(chandler);
            mushroom = new JCheckBox("Mushroom");
            mushroom.addItemListener(chandler);
            pineapple = new JCheckBox("Pineapple");
            pineapple.addItemListener(chandler);
            


        
            panel5.add(sausage);
            panel5.add(pepperoni);    
            panel5.add(cheese);    
            panel5.add(ham);    
            panel5.add(olive);                
            panel5.add(mushroom);
            panel5.add(pineapple);
            
            panel6.add(toppings,BorderLayout.NORTH);
            
            panel2.add(panel6,BorderLayout.WEST);
            panel2.add(panel5,BorderLayout.CENTER);



            crust = new ButtonGroup();
            
            c=new JLabel("Crust");
            h = new JRadioButton("Hand-tossed");
            h.addActionListener(rhandler);
             t = new JRadioButton("Thin-crust");
             t.addActionListener(rhandler);
            d = new JRadioButton("Deep Dished");
            d.addActionListener(rhandler);

            crust.add(h);
            crust.add(t);
            crust.add(d);
            

            panel4.add(c);
            panel4.add(h);
            panel4.add(t);
            panel4.add(d);
            
            


            discount1=new JLabel("   Discount");
            discount2=new JLabel("           If Yes,");
            discount3=new JLabel("%");
            
            
            yes=new JRadioButton("Yes");
            no=new JRadioButton("No");
            dcount=new JTextField(5);
            dcount.setText(""+0.00);
            
            dscnt=new ButtonGroup();
            
            dscnt.add(yes);
            dscnt.add(no);
            


            panel11.add(discount1);
            panel11.add(yes);
            panel11.add(no);
            panel11.add(discount2);
            panel11.add(dcount);
            panel11.add(discount3);
            
            panel12.add(panel11,BorderLayout.WEST);


            ButtonEventHandler  beh=new ButtonEventHandler();    
            reset=new JButton("Reset");
            reset.addActionListener(beh);
            add=new JButton("Add");
            
            add.addActionListener(beh);
            
    
            panel7.add(reset);
            panel7.add(add);
            panel8.add(panel7,BorderLayout.EAST);


            webeei=new JLabel("\t\tQty\t\tSize\t\t\t\tPrice\t\tSubtotal");
            lapad=new JTextArea(60,60);
            lapad.setLineWrap(true);
            iskrol=new JScrollPane(lapad);
        

            panel13.add(webeei,BorderLayout.NORTH);
    
            panel13.add(iskrol,BorderLayout.CENTER);
            

            
            totAMNT=new JLabel("Total Amount");
            tenamt=new JLabel("Tendered Amount");
            change=new JLabel("Change");
            
            tot=new JTextField(10);
            ten=new JTextField(10);
            TextFieldHandler th=new TextFieldHandler();
            ten.addActionListener(th);
            cha=new JTextField(10);
            

            panel9.add(totAMNT);
            panel9.add(tot);
            panel9.add(tenamt);
            panel9.add(ten);
            panel9.add(change);
            panel9.add(cha);
            panel10.add(panel9,BorderLayout.EAST);
        
            
            
        
            add(panel1);
            add(panel2);
            add(panel4);
            add(panel12);
            add(panel8);
            add(panel13);
            add(panel10);
            setVisible(true);
            add(out);
                
        }
        class ComboHandler implements ItemListener{
            public void itemStateChanged(ItemEvent eventc){
                if(eventc.getStateChange()==ItemEvent.SELECTED){
                    mambo=thecombo.getSelectedIndex();
                    String s2=Integer.toString(mambo);
                    //number holds the quantity
                    number=bebang[mambo];
                    }
            }
        }
        class CheckboxHandler implements ItemListener{
            public void itemStateChanged(ItemEvent event){
            
                if (event.getSource()==sausage){
                    tsekbaks=sausage.isSelected() ? 5 : 0;}
                if (event.getSource()==pepperoni){
                    tseksbaksB=pepperoni.isSelected() ? 10 : 0;}
                if (event.getSource()==cheese){
                    tseksbaksC=cheese.isSelected() ? 15 : 0;}
                if (event.getSource()==ham){
                    tseksbaksD=ham.isSelected() ? 20 : 0;}
                if (event.getSource()==olive){
                    tseksbaksE=olive.isSelected() ? 30 : 0;}
                if (event.getSource()==mushroom){
                    tseksbaksF=mushroom.isSelected() ? 40 : 0;}
                if (event.getSource()==pineapple){
                    tseksbaksG=pineapple.isSelected() ? 50 : 0;}
                tsekbaksA=tsekbaksA+(tsekbaks+tseksbaksB+tseksbaksC+tseksbaksD+tseksbaksE+tseksbaksF+tseksbaksG);
                
                }
                }
        class RadioHandler implements ActionListener{
            public void actionPerformed(ActionEvent event1){
        
            if(event1.getActionCommand()=="Small"){
                    s="Small";
                    says=0;
                    says=says+400;
                }
             if(event1.getActionCommand()=="Medium"){
                    s="Medium";
                    says=0;
                    says=says+500;
                }
            if(event1.getActionCommand()=="Large"){
                    s="Large";
                    says=0;
                    says=says+600;
                }
            if(event1.getActionCommand()=="Family Size"){
                    s="Family Size";
                    says=0;
                    says=says+700;
                }
        
            if(event1.getActionCommand()=="Hand-tossed"){
                     istilo="Hand-tossed";
                }
            if(event1.getActionCommand()=="Thin-crust"){
                     istilo="Thin-crust";
                }
            if(event1.getActionCommand()=="Deep Dished"){
                     istilo="Deep Dished";
                }
                
            if(event1.getActionCommand()=="Yes"){
                
                }
            }
        }
        private class ButtonEventHandler implements ActionListener{
            public void actionPerformed(ActionEvent e){
            
                String str=e.getActionCommand();
                
                    if (str.equals("Add")){
                    //holds the discount
                    disc=Double.parseDouble(dcount.getText())/100;
                    D=(number*(says+tsekbaksA)*disc);
                    subtotal=number*(says+tsekbaksA)-D;
                    price=says+tsekbaksA;
                    lapad.append(" "+number+" "+s+" "+istilo+" "+price+" "+subtotal+"\n");
                    total+=subtotal;
                    tot.setText(Double.toString(total));        
                    tsekbaksA=0;
                    disc=0.0;
                    subtotal=0.0;
                    price=0.0;
                    D=0.0;
                    
                }
                
                else if (str.equals("Reset")){
                    
                    
                    thecombo.setSelectedItem(0);
                    yes.setSelected(false);
                    no.setSelected(false);
                    small.setSelected(false);
                    medium.setSelected(false);
                    large.setSelected(false);
                    famsize.setSelected(false);
                    sausage.setSelected(false);
                    pepperoni.setSelected(false);
                    cheese.setSelected(false);
                    ham.setSelected(false);
                    olive.setSelected(false);
                    mushroom.setSelected(false);
                    pineapple.setSelected(false);