mehdix_
mehdix_

Reputation: 479

Swing: Panel Size Issue

I am designing an application, in which there should be 5 different JPanels containing different Swing components. For the JRadioButton part, I ran into an issue for which couldn't find proper solution. The 'radioSizePanel' is supposed to be placed somewhere in upper middle of the main panel. Has anyone solved this problem before ? Here is the code, that I am using :

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

public class PizzaShop extends JFrame 
{
    private static final long serialVersionUID = 1L;
    private JRadioButton[] radio_size = new JRadioButton[3];
    private JRadioButton[] radio_type = new JRadioButton[3];
    public PizzaShop() 
    {
        initializaUI();
    }

    private void initializaUI()
    {
        setSize(700, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Panel container to wrap checkboxes and radio buttons
        JPanel panel = new JPanel();

        //sizes radio buttons
        String Size[] = {"Small: $6.50", "Medium: $8.50", "Large: $10.00"};
        JPanel radioSizePanel = new JPanel(new GridLayout(3, 1));
        ButtonGroup radioSizeGroup = new ButtonGroup();
        for (int i=0; i<3; i++)
        {
            radio_size[i] = new JRadioButton(Size[i]);
            radioSizePanel.add(radio_size[i]);
            radioSizeGroup.add(radio_size[i]);
        }
        radioSizePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Size"));
        radioSizePanel.setPreferredSize(new Dimension(100, 200));

        //
        panel.add(radioSizePanel);
        setContentPane(panel);
        setContentPane(radioSizePanel);
    }

    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new PizzaShop().setVisible(true);
            }
        });
    }
}

Here is what I want as an expected OUTPUT :

EXPECTED OUTPUT

Upvotes: 1

Views: 1090

Answers (1)

nIcE cOw
nIcE cOw

Reputation: 24626

Please do watch the code example and Please do watch everything carefully , the sequence of adding things to the JFrame. Since in your example you calling setSize() much before something has been added to the JFrame, hence first add components to the container, and then call it's pack()/setSize() methods, so that it can realize that in a good way.

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

public class PizzaLayout
{
    /*
     * Five JPanels we will be using.
     */
    private JPanel headerPanel;
    private JPanel footerPanel;
    // This JPanel will contain the middle components.
    private JPanel centerPanel;
    private JPanel toppingPanel;
    private JPanel sizePanel;
    private JPanel typePanel;
    private JPanel buttonPanel;

    private String[] toppings = {
                                    "Tomato",
                                    "Green Pepper",
                                    "Black Olives",
                                    "Mushrooms",
                                    "Extra Cheese",
                                    "Pepproni",
                                    "Sausage"
                                };

    private JCheckBox[] toppingsCBox;

    private String[] sizePizza = {
                                    "Small $6.50", 
                                    "Medium $8.50",
                                    "Large $10.00"
                                 };

    private JRadioButton[] sizePizzaRButton;

    private String[] typePizza = {
                                    "Thin Crust", 
                                    "Medium Crust",
                                    "Pan"
                                 };

    private JRadioButton[] typePizzaRButton;

    private JButton processButton;

    private ButtonGroup bGroupType, bGroupSize;

    private JTextArea orderTArea;

    private StringBuilder sBuilderOrder;

    private void displayGUI()
    {
        JFrame frame = new JFrame("Pizza Shop");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*
         * This JPanel is the base of all the
         * other components, and at the end 
         * we will set this as Content Pane
         * for the JFrame.
         */
        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.setBorder(
            BorderFactory.createEmptyBorder(10, 10, 10, 10));
        /*
         * TOP PART of the LAYOUT.
         */
        headerPanel = new JPanel();
        JLabel headerLabel = new JLabel(
                "Welcome to Home Style Pizza Shop"
                                    , JLabel.CENTER);
        headerLabel.setForeground(Color.RED);
        headerPanel.add(headerLabel);

        /*
         * CENTER PART of the LAYOUT.
         */
        centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.weightx = 0.3;
        gbc.weighty = 1.0;

        /*
         * Above Constraints are for this part.
         */
        toppingPanel = new JPanel();
        toppingPanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Each Topping $1.50"));
        JPanel checkBoxesPanel = new JPanel();
        checkBoxesPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        checkBoxesPanel.setLayout(new GridLayout(0, 1, 5, 5));

        toppingsCBox = new JCheckBox[toppings.length];
        for (int i = 0; i < toppings.length; i++)
        {
            toppingsCBox[i] = new JCheckBox(toppings[i]);
            checkBoxesPanel.add(toppingsCBox[i]);
        }

        toppingPanel.add(checkBoxesPanel);
        centerPanel.add(toppingPanel, gbc);

        // Till this.

        gbc.gridx = 1;
        gbc.gridheight = 1;
        gbc.weighty = 0.7;

        /*
         * Above Constraints are for this part.
         */
        sizePanel = new JPanel();
        sizePanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Pizza Size"));
        JPanel radioBoxesPanel = new JPanel();
        radioBoxesPanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        radioBoxesPanel.setLayout(new GridLayout(0, 1, 10, 10));

        sizePizzaRButton = new JRadioButton[sizePizza.length];
        bGroupSize = new ButtonGroup();
        for (int i = 0; i < sizePizza.length; i++)
        {
            sizePizzaRButton[i] = new JRadioButton(sizePizza[i]);
            bGroupSize.add(sizePizzaRButton[i]);
            radioBoxesPanel.add(sizePizzaRButton[i]);
        }

        sizePanel.add(radioBoxesPanel);
        centerPanel.add(sizePanel, gbc);
        // Till this.

        gbc.gridx = 2;
        gbc.weighty = 0.7;

        /*
         * Above Constraints are for this part.
         */
        typePanel = new JPanel();
        typePanel.setBorder(
            BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.RED),
                                            "Pizza Type"));
        JPanel radioBoxesTypePanel = new JPanel();
        radioBoxesTypePanel.setBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5));
        radioBoxesTypePanel.setLayout(new GridLayout(0, 1, 10, 10));

        typePizzaRButton = new JRadioButton[typePizza.length];
        bGroupType = new ButtonGroup();
        for (int i = 0; i < typePizza.length; i++)
        {
            typePizzaRButton[i] = new JRadioButton(typePizza[i]);
            bGroupType.add(typePizzaRButton[i]);
            radioBoxesTypePanel.add(typePizzaRButton[i]);
        }

        typePanel.add(radioBoxesTypePanel);
        centerPanel.add(typePanel, gbc);
        // Till this.

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weighty = 0.3;
        gbc.gridwidth = 2;

        processButton = new JButton("Process Selection");
        processButton.addActionListener(
            new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                sBuilderOrder = new StringBuilder();
                sBuilderOrder.append("Pizza type : ");
                for (int i = 0; i < typePizza.length; i++)
                {
                    if (typePizzaRButton[i].isSelected())
                        sBuilderOrder.append(typePizzaRButton[i].getText());
                }

                sBuilderOrder.append("\n");
                sBuilderOrder.append("Pizza Size : ");

                for (int i = 0; i < sizePizza.length; i++)
                {
                    if (sizePizzaRButton[i].isSelected())
                        sBuilderOrder.append(sizePizzaRButton[i].getText());
                }

                sBuilderOrder.append("\n");
                sBuilderOrder.append("Toppings : ");
                /*
                 * I hope you can do this part yourself now :-)
                 */

                orderTArea.setText(sBuilderOrder.toString());
            }
        });
        centerPanel.add(processButton, gbc);

        footerPanel = new JPanel();
        footerPanel.setLayout(new BorderLayout(5, 5));
        footerPanel.setBorder(
            BorderFactory.createTitledBorder("Your Order : "));
        orderTArea = new JTextArea(10, 10);
        footerPanel.add(orderTArea, BorderLayout.CENTER);

        contentPane.add(headerPanel, BorderLayout.PAGE_START);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new PizzaLayout().displayGUI();
            }
        });
    }
}

Here is the output of the same :

PIZZALAYOUT

Upvotes: 2

Related Questions