Search code examples
javaswinglayoutlayout-managerboxlayout

BorderLayout doesn't honor maximumsize


I have a problem using BorderLayout, but first of all, here is my GUI setup:

enter image description here

As you can see, I have 3 different components inside my JFrame. Adding the JMenu and JList works fine. But my JPanel should have a fixed size so I want to prevent my BorderLayout from stretching the panel. I tried everything, setPreferredSize() setMinimumSize() setMaximumSize() setSize() but again the layout stretches my panel to fit to the frame. (The panel is added to the frame using BorderLayout.CENTER).

Is there any way to prevent this or do you have other suggestions to manage the problem?


Solution

  • I'm pretty sure you mean BorderLayout, not BoxLayout, because there is no BoxLayout.CENTER and it looks like you use a BorderLayout to place the components.

    I think the problem here is that you only set the preferred size of the panel that you add to BorderLayout.CENTER. This doesn't have any effect. Instead you need nested layouts.

    In this example I added the JPanel called centerPanel, which is using a standard GridBagLayout (to center the added component), to BorderLayout.CENTER. Then I added the additional JPanel called panel, which has a custom preferrdSize, to centerPanel. This way panel won't get stretched.

    enter image description here


    Code:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    
    public class Example {
    
        public Example() {
            JMenuBar menuBar = new JMenuBar();
            menuBar.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
    
            DefaultListModel<String> listModel = new DefaultListModel<String>();
            JList<String> list = new JList<String>(listModel);
            list.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
    
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(400, 400);
                }
                // Uncomment the following lines if you also want to prevent the
                // 'wrapping' of the panel.
                /*
                 * @Override public Dimension getMinimumSize() { return new
                 * Dimension(400, 400); }
                 */
            };
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
    
            for (int i = 1; i <= 5; i++) {
                menuBar.add(new JMenu("Menu " + i));
                listModel.addElement("Element " + i);
                panel.add(new JLabel("Label " + i));
            }
    
            JPanel centerPanel = new JPanel(new GridBagLayout());
            centerPanel.add(panel);
    
            JPanel contentPanel = new JPanel(new BorderLayout());
            contentPanel.add(menuBar, BorderLayout.NORTH);
            contentPanel.add(list, BorderLayout.WEST);
            contentPanel.add(centerPanel);
    
            JFrame frame = new JFrame();
            frame.setContentPane(contentPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Example();
                }
            });
        }
    
    }