Search code examples
javaswinglayout-managerborder-layoutboxlayout

Issues with BoxLayout in a subpanel


I am writing a program for a mock pizza company, and I get a BoxLayout cannot be shared error when I run my code. I am new to Layout managers and have only used Flow prior.

I was working in this method at the time and I included the rest of the class below, but if you need to see more, let me know.

/***** Pizza Method *****/
private JPanel Pizza()   {

    /*Creates Pizza Panel*/
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());

    JPanel center = new JPanel();
    center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    /*Buffer Panel*/
    JPanel buffer = new JPanel();
    buffer.setPreferredSize(new Dimension(20, 30));

    /*Title*/
    JLabel title = new JLabel("Signature Pizzas: ");

    /*** Pizzas ***/
    JLabel pepperoni = new JLabel("Pepperoni = Good Ol' Cheese and Pepperoni ----- $5");
    JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
            + " Ham, and Bacon with American Cheese ----- $10");

    /*** Add Components ***/
    center.add(pepperoni);
    center.add(Box.createVerticalGlue());
    center.add(allAmerican);

    panel.add(title, BorderLayout.NORTH);
    panel.add(buffer, BorderLayout.WEST);
    panel.add(new JPanel(), BorderLayout.SOUTH);
    panel.add(center, BorderLayout.CENTER);

    return panel;
}

Here is the Class

package pizza;

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

public class SigDishPanel extends JPanel {

    public SigDishPanel()   {
        /*** Set Defaults***/
        setLayout(new GridLayout(2, 1));
        setBackground(Color.ORANGE);

        /*** Adds Pizza and Drinks Panels ***/
        add(Pizza());
        add(Drinks());
    }



    /***** Pizza Method *****/
    private JPanel Pizza()   {

        /*Creates Pizza Panel*/
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel center = new JPanel();
        center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        /*Buffer Panel*/
        JPanel buffer = new JPanel();
        buffer.setPreferredSize(new Dimension(20, 30));

        /*Title*/
        JLabel title = new JLabel("Signature Pizzas: ");

        /*** Pizzas ***/
        JLabel pepperoni = new JLabel("Pepperoni = Good Ol' Cheese and Pepperoni ----- $5");
        JLabel allAmerican = new JLabel("All American = Pepperoni, Ground Beef,"
                + " Ham, and Bacon with American Cheese ----- $10");

        /*** Add Components ***/
        center.add(pepperoni);
        center.add(Box.createVerticalGlue());
        center.add(allAmerican);

        panel.add(title, BorderLayout.NORTH);
        panel.add(buffer, BorderLayout.WEST);
        panel.add(new JPanel(), BorderLayout.SOUTH);
        panel.add(center, BorderLayout.CENTER);

        return panel;
    }



    /***** Drinks Method *****/
    private JPanel Drinks()   {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel buffer = new JPanel();
        buffer.setPreferredSize(new Dimension(20, 30));

        JLabel title = new JLabel("Signature Beverages: ");

        /*** Drinks ***/
        JLabel bearBrew = new JLabel("Bear Claw Brew ----- $2");

        /*** Add Components ***/
        panel.add(title, BorderLayout.NORTH);
        panel.add(buffer, BorderLayout.WEST);

        panel.add(bearBrew, BorderLayout.CENTER);

        return panel;
    }
}

Solution

  • You're setting center's layout with the BoxLayout and thus need to pass it into the BoxLayout constructor. So not this:

    center.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    

    but rather this:

    center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));