Search code examples
javaswinglayout-managergridbaglayoutjradiobutton

JRadioButton is not show properly


I want to create a simple food ordering system, now i'm creating the interface of an order form. I used GridBagLayout for create the form layout, my problem is when I want to assign 3 radio button in same row, it's only show me 1 of the button....I hope somebody can help me pls....

Here is my java code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/**
 *
 * @author user
 */
public class ChickenChopOrderingSystem
{
    JFrame frame;
    JPanel mainPanel, p1, p2, p3, p4;
    JLabel lblTitle, lblName, lblPhoneNum, lblFlavour, lblChickenPart;
    JTextField txtName, txtPhoneNum;
    String flavour[] = {"Black Pepper Sauce", "Hainanese", "Grilled", "Lemon"};
    JComboBox box;
    ButtonGroup bg = new ButtonGroup();
    JRadioButton btnWhole, btnHalf, btnQuarter;
    JButton btnDone, btnExit;

    public ChickenChopOrderingSystem()
    {
        frame = new JFrame("Chicken Chop Ordering System");
        mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(700,700));
        mainPanel.setBackground(Color.yellow);

        lblName = new JLabel("Customer's Name: ");
            txtName = new JTextField(20);

        lblPhoneNum = new JLabel("Phone Number: ");
            txtPhoneNum = new JTextField(11);

        lblChickenPart = new JLabel("Select Part of Chicken: ");
            btnWhole = new JRadioButton("Whole");
                btnWhole.addItemListener(new OperationListener());
            btnHalf = new JRadioButton("Half");
                btnHalf.addItemListener(new OperationListener());
            btnQuarter = new JRadioButton("Quarter");
                btnQuarter.addItemListener(new OperationListener());
            bg.add(btnWhole);
            bg.add(btnHalf);
            bg.add(btnQuarter);

        lblFlavour = new JLabel("Select a flavour: ");
            box = new JComboBox(flavour);

        btnDone = new JButton("Done");
        btnExit = new JButton("Exit");
            btnExit.addActionListener(new ButtonListener());

        //GridBaglayout
        mainPanel.setLayout(new GridBagLayout()); 
        GridBagConstraints gbc = new GridBagConstraints();

        //Label
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.5;
        gbc.weighty = 0.5;
        mainPanel.add(lblName, gbc);


        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.5;
        mainPanel.add(lblPhoneNum, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.5;        
        mainPanel.add(lblChickenPart, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.weightx = 0.5;
        mainPanel.add(lblFlavour, gbc);

        //TextField
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        mainPanel.add(txtName, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        mainPanel.add(txtPhoneNum, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 2;
        mainPanel.add(btnWhole, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        mainPanel.add(btnHalf, gbc);

        gbc.gridx = 3;
        gbc.gridy = 2;
        mainPanel.add(btnHalf, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 3;
        mainPanel.add(box, gbc);


        //frame setting
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridBagLayout());
        frame.add(mainPanel, new GridBagConstraints());
        frame.setSize(new Dimension(1000, 1000));
        frame.setVisible(true);
    }


    public class OperationListener implements ItemListener
    {
        @Override
        public void itemStateChanged(ItemEvent ie) {
            if (ie.getSource() == btnWhole) 
            {                 
                if (ie.getStateChange() == ItemEvent.SELECTED) 
                {   
                    box.removeAllItems(); 
                    box.addItem(flavour[2]);
                }


            } if (ie.getSource() == btnHalf)
            {                 
                if (ie.getStateChange() == ItemEvent.SELECTED) 
                {   
                    box.removeAllItems(); 
                    box.addItem(flavour[0]);
                    box.addItem(flavour[2]);
                    box.addItem(flavour[3]);
                }

            } if (ie.getSource() == btnQuarter)
            {
                if (ie.getStateChange() == ItemEvent.SELECTED) 
                {   
                    box.removeAllItems(); 
                    box.addItem(flavour[0]);
                    box.addItem(flavour[1]);
                    box.addItem(flavour[3]);              
                }
            }
        }
    }

    public class ButtonListener implements ActionListener
    {
        @Override

        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == btnExit)
            {
                int s = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?",              
                    "Exit", JOptionPane.YES_NO_OPTION);      

                if (s == JOptionPane.YES_OPTION) 
                {            
                    System.exit(0);         
                } 
            }
        }

    }

    public static void main(String[] args)
    {
        ChickenChopOrderingSystem run = new ChickenChopOrderingSystem();
    }
}

Click here to view output


Solution

  • For something like this:

    enter image description here

    Use this code:

    import java.awt.*;
    import javax.swing.*;
    
    public class ChickenChopOrderingSystem {
    
        JFrame frame;
        JPanel mainPanel, p1, p2, p3, p4;
        JLabel lblTitle, lblName, lblPhoneNum, lblFlavour, lblChickenPart;
        JTextField txtName, txtPhoneNum;
        String flavour[] = {"Black Pepper Sauce", "Hainanese", "Grilled", "Lemon"};
        JComboBox box;
        ButtonGroup bg = new ButtonGroup();
        JRadioButton btnWhole, btnHalf, btnQuarter;
        JButton btnDone, btnExit;
    
        public ChickenChopOrderingSystem() {
            frame = new JFrame("Chicken Chop Ordering System");
            mainPanel = new JPanel();
            // GUESSWORK! 
            //mainPanel.setPreferredSize(new Dimension(700,700));
            mainPanel.setBackground(Color.yellow);
    
            lblName = new JLabel("Customer's Name: ");
            txtName = new JTextField(20);
    
            lblPhoneNum = new JLabel("Phone Number: ");
            txtPhoneNum = new JTextField(11);
    
            lblChickenPart = new JLabel("Select Part of Chicken: ");
            btnWhole = new JRadioButton("Whole");
            btnHalf = new JRadioButton("Half");
            btnQuarter = new JRadioButton("Quarter");
            bg.add(btnWhole);
            bg.add(btnHalf);
            bg.add(btnQuarter);
    
            lblFlavour = new JLabel("Select a flavour: ");
            box = new JComboBox(flavour);
    
            btnDone = new JButton("Done");
            btnExit = new JButton("Exit");
    
            //GridBaglayout
            mainPanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            int s = 20;
            gbc.insets = new Insets(s,s,s,s);
    
            //Label
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 0.5;
            gbc.weighty = 0.5;
            mainPanel.add(lblName, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.weightx = 0.5;
            mainPanel.add(lblPhoneNum, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.weightx = 0.5;
            mainPanel.add(lblChickenPart, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.weightx = 0.5;
            mainPanel.add(lblFlavour, gbc);
    
            //TextField
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 0;
            gbc.gridwidth = 3;
            mainPanel.add(txtName, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 3;
            mainPanel.add(txtPhoneNum, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 2;
            gbc.gridwidth = 1;
            gbc.weightx = 1d/6d;
            mainPanel.add(btnWhole, gbc);
    
            gbc.gridx = 2;
            gbc.gridy = 2;
            mainPanel.add(btnHalf, gbc);
    
            gbc.gridx = 3;
            gbc.gridy = 2;
            mainPanel.add(btnQuarter, gbc);
    
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridx = 1;
            gbc.gridy = 3;
            gbc.gridwidth = 3;
            mainPanel.add(box, gbc);
    
            //frame setting
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridBagLayout());
            frame.add(mainPanel, new GridBagConstraints());
            // GUESSWORK! 
            //frame.setSize(new Dimension(1000, 1000));
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            ChickenChopOrderingSystem run = new ChickenChopOrderingSystem();
        }
    }
    

    The problems in the original code were many. (Trawling memory..)

    • The constraints of the last element were not set back to grid width of 3, confusing the layout manager.
    • The ItemListener was doing strange stuff with removing components, don't do that.
    • The preferred size of the panel, and the size of the frame, were guesswork. Use pack() to have the correct size calculated. (Add a standard Inserts to the initial constraints for white space.)