Search code examples
javajpanelborder-layout

Can I have 5 panels in a row


Is it possible to have 5 JPanels in a row in a single Frame? Heres my code you may get a better understanding of the question:

setLayout(new BorderLayout());
add(mainOrderInfo, BorderLayout.WEST);
add(meatPanel, BorderLayout.WEST);
add(cheesePanel, BorderLayout.CENTER);
add(ingrediantsPanel, BorderLayout.EAST);
add(addonsPanel, BorderLayout.EAST);

When the code is ran all I get are the meatPanel, cheesePanel, and ingrediantsPanel. I would like the mainOrderInfo to be to the left of the meatPanel and the addonsPanel to the right of the ingredientsPanel. I feel as though its simple a matter of code or ordering of code that I am not aware of.


Solution

  • GridLayout

    See How to Use GridLayout for more details

    GridLayout

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridLayout(1, 5));
                add(createPanel(Color.WHITE));
                add(createPanel(Color.BLACK));
                add(createPanel(Color.RED));
                add(createPanel(Color.GREEN));
                add(createPanel(Color.BLUE));
            }
    
            protected JPanel createPanel(Color color) {
                JPanel panel = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(50, 50);
                    }
                };
                panel.setBackground(color);
                return panel;
            }
    
        }
    
    }
    

    GridBagLayout

    See How to Use GridBagLayout for more details

    GridBagLayout

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(createPanel(Color.WHITE), gbc);
                gbc.gridx++;
                add(createPanel(Color.BLACK), gbc);
                gbc.gridx++;
                add(createPanel(Color.RED), gbc);
                gbc.gridx++;
                add(createPanel(Color.GREEN), gbc);
                gbc.gridx++;
                add(createPanel(Color.BLUE), gbc);
            }
    
            protected JPanel createPanel(Color color) {
                JPanel panel = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(50, 50);
                    }
                };
                panel.setBackground(color);
                return panel;
            }
    
        }
    
    }