Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Grid bag layout inside grid bag layout

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Grid bag layout inside grid bag layout

    As the subject suggests, I am attempting to put a grid bag layout inside another grid bag layout. It's turned out horribly wrong which you will see by running the code I have. What I want is to put the toppings panel in its own separate grid bag layout, but it is overlapping the rest of the GUI. I'm not really sure what the problem is.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class PizzaCalculator
    {
     
        public static void main(String[] args)
        {
        	JFrame frame = new PizzaFrame();
        	frame.setVisible(true);
        }
     
    }
     
    class PizzaFrame extends JFrame
    {
    	public PizzaFrame()
    	{
    		setTitle("Pizza Calculator");
    		setResizable(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new PizzaPanel();
    		this.add(panel);
    		this.pack();
    		centerWindow(this);
    	}
     
    	private void centerWindow(Window w)
    	{
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		Dimension d = tk.getScreenSize();
    		setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    	}
    }
     
    class PizzaPanel extends JPanel implements ActionListener
    {
    	private JRadioButton 	smallPizzaRadioButton,
    							mediumPizzaRadioButton,
    							largePizzaRadioButton,
    							clearRadioButton;
     
    	private JCheckBox		sausageCheckBox,
    							pepperoniCheckBox,
    							salamiCheckBox,
    							hamburgerCheckBox,
    							olivesCheckBox,
    							mushroomsCheckBox,
    							peppersCheckBox,
    							onionsCheckBox;
     
    	private JTextField		priceTextField;
    	private JLabel			priceLabel;
     
    	private JButton			calculateButton,
    							clearButton,
    							exitButton;
     
    	public PizzaPanel()
    	{
    		setLayout(new GridBagLayout());
     
    		Border loweredBorder = BorderFactory.createEtchedBorder();
     
    		// radio button panel
    		JPanel radioPanel = new JPanel();
    		ButtonGroup sizeGroup = new ButtonGroup();
            radioPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
            radioPanel.setBorder(BorderFactory.createTitledBorder(loweredBorder, "Size: "));
     
            // small pizza radio button
            smallPizzaRadioButton = new JRadioButton("Small Pizza");
            smallPizzaRadioButton.addActionListener(this);
            sizeGroup.add(smallPizzaRadioButton);
            radioPanel.add(smallPizzaRadioButton);
     
            // medium pizza radio button
     
    		mediumPizzaRadioButton = new JRadioButton("Medium Pizza");
            mediumPizzaRadioButton.addActionListener(this);
            sizeGroup.add(mediumPizzaRadioButton);
            radioPanel.add(mediumPizzaRadioButton);
     
            // large pizza radio button
            largePizzaRadioButton = new JRadioButton("Large Pizza");
            largePizzaRadioButton.addActionListener(this);
            sizeGroup.add(largePizzaRadioButton);
            radioPanel.add(largePizzaRadioButton);
     
            // initialize radio button position
            clearRadioButton = new JRadioButton("Clear", true);
            clearRadioButton.addActionListener(this);
     
            add(radioPanel, getConstraints(0,0,3,1, GridBagConstraints.CENTER));
     
            /*************************************************************************************/
     
            /* THIS IS WHERE I ATTEMPT TO CREATE A NEW GRID BAG LAYOUT INSIDE THE CURRENT GBL */
     
            JPanel topPan = new JPanel();
            ButtonGroup toppingsGroup = new ButtonGroup();
            topPan.setLayout(new GridBagLayout());
            topPan.setBorder(BorderFactory.createTitledBorder(loweredBorder, "Toppings "));
            add(topPan, getConstraints(1,1,2,1, GridBagConstraints.CENTER));
     
     
            // sausage check box
    		sausageCheckBox = new JCheckBox("Sausage");
    		toppingsGroup.add(sausageCheckBox);
    		topPan.add(sausageCheckBox);
    		add(sausageCheckBox, getConstraints(0,0,1,1, GridBagConstraints.WEST));
     
    		// pepperoni check box
    		pepperoniCheckBox = new JCheckBox("Pepperoni");
    		toppingsGroup.add(pepperoniCheckBox);
    		topPan.add(pepperoniCheckBox);
    		add(pepperoniCheckBox, getConstraints(0,1,1,1, GridBagConstraints.WEST));
     
    		// salami check box
    		salamiCheckBox = new JCheckBox("Salami");
    		toppingsGroup.add(salamiCheckBox);
    		topPan.add(salamiCheckBox);
    		add(salamiCheckBox, getConstraints(0,2,1,1, GridBagConstraints.WEST));
     
    		// hamburger check box
    	    hamburgerCheckBox = new JCheckBox("Hamburger");
    	    toppingsGroup.add(hamburgerCheckBox);
    		topPan.add(hamburgerCheckBox);
    	    add(hamburgerCheckBox, getConstraints(0,3,1,1, GridBagConstraints.WEST));
     
    	    // olives check box
    	    olivesCheckBox = new JCheckBox("Olives");
    	    toppingsGroup.add(olivesCheckBox);
    		topPan.add(olivesCheckBox);
    	    add(olivesCheckBox, getConstraints(1,0,1,1, GridBagConstraints.WEST));
     
    	    // mushrooms check box
    	    mushroomsCheckBox = new JCheckBox("Mushrooms");
    	    toppingsGroup.add(mushroomsCheckBox);
    		topPan.add(mushroomsCheckBox);
    	    add(mushroomsCheckBox, getConstraints(1,1,1,1, GridBagConstraints.WEST));
     
    	    // peppers check box
    	    peppersCheckBox = new JCheckBox("Peppers");
    	    toppingsGroup.add(mushroomsCheckBox);
    		topPan.add(mushroomsCheckBox);
    	    add(peppersCheckBox, getConstraints(1,2,1,1, GridBagConstraints.WEST));
     
    	    // onions check box
    	    onionsCheckBox = new JCheckBox("Onions");
    	    toppingsGroup.add(onionsCheckBox);
    		topPan.add(onionsCheckBox);
    	    add(onionsCheckBox, getConstraints(1,3,1,1, GridBagConstraints.WEST));
     
     
    		/*************************************************************************/
     
            // price label
    		priceLabel = new JLabel("Price:");
    		add(priceLabel, getConstraints(0,2,1,1, GridBagConstraints.EAST));
     
    		// price text field
    		priceTextField = new JTextField(10);
    		add(priceTextField, getConstraints(1,2,1,1, GridBagConstraints.WEST));
     
    		// calculate button
    		calculateButton = new JButton("Calculate");
    		calculateButton.addActionListener(this);
    		add(calculateButton, getConstraints(0,3,1,1, GridBagConstraints.CENTER));
     
    		// clear button
    		clearButton = new JButton("Clear");
    		clearButton.addActionListener(this);
    		add(clearButton, getConstraints(1,3,1,1, GridBagConstraints.CENTER));
     
    		// exit button
    		exitButton = new JButton("Clear");
    		exitButton.addActionListener(this);
    		add(exitButton, getConstraints(2,3,1,1, GridBagConstraints.CENTER));
     
    	}
     
    	// a method for setting grid bag constraints
    	private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor)
    	{
    		GridBagConstraints c = new GridBagConstraints();
    		c.insets = new Insets(5, 5, 5, 5);
    		c.ipadx = 0;
    		c.ipady = 0;
    		c.gridx = gridx;
    		c.gridy = gridy;
    		c.gridwidth = gridwidth;
    		c.gridheight = gridheight;
    		c.anchor = anchor;
    		return c;
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    	}
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Grid bag layout inside grid bag layout

    Can you kindly just post the code, with which you are having trouble, it's too odd to look through the whole. Thanks

Similar Threads

  1. Layout manager
    By kurt-hardy in forum AWT / Java Swing
    Replies: 3
    Last Post: January 19th, 2011, 10:25 AM
  2. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM
  3. How to align the items in a form (grid layout)?
    By onlybarca in forum AWT / Java Swing
    Replies: 4
    Last Post: November 27th, 2010, 11:38 PM
  4. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM
  5. My Custom Layout (VERY EASY TO USE)
    By aussiemcgr in forum AWT / Java Swing
    Replies: 10
    Last Post: August 5th, 2010, 01:37 PM