0

For some reason my JPanels that I'm adding to the content JPanel are stretching vertically, instead of being the preferred size. Here's the code:

    public static JPanel content;

    public APP()
    {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setJMenuBar(new MenuBar());

        content = new JPanel(new GridLayout(0, 8, 2, 2));
        add(new JScrollPane(content, 22, 32), BorderLayout.CENTER);

        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);

        addJPanel();
        addJPanel();
    }

    public void addJPanel()
    {
        JPanel box = new JPanel();
        box.setPreferredSize(new Dimension(100, 100));
        box.setBackground(Color.BLACK);
        content.add(box);
        content.validate();
    }

How can I keep them at a specific size when they're being added? Or at least keep them from stretching all the way across the content panel. I want them to be all the same size without changing their sizes as more panels are added.

*NOTE: addJPanel() allows me to dynamically add new panels into the content panel with a button or an action. It is only a test method, the real addition of panels come from an action called by the menu button.

B.K.
  • 9,418
  • 9
  • 64
  • 98
  • 3
    Don't use BorderLayout, if you don't want it to stretch. Maybe switch to `GirdBagLayout` which will center your component and keep their preferredsize – Guillaume Polet May 31 '13 at 12:32
  • @GuillaumePolet Well, BorderLayout is used on the JFrame, JPanel is the one that uses GridLayout, and things are being added to it, so BorderLayout should have no effect on it. And it doesn't. If I delete the BorderLayout lines nothing will change. – B.K. May 31 '13 at 12:57
  • @Guillaume Polet not wrong suggestion, this container is excelently layed, JFrame with (in the CENTER area) JScrollPane with/nested JPanel, best of options .... – mKorbel May 31 '13 at 13:05
  • @mKorbel I don't understand what you mean. – B.K. May 31 '13 at 13:18
  • @Noobacode The default LayoutManager of the content pane is BorderLayout, so removing your line will not change anything. Try `setLayout(new GridBagLayout());` – Guillaume Polet May 31 '13 at 17:01

1 Answers1

1

I ended up figuring out a solution using glue for the box layout, since I really wanted to use GridLayout to make things simple for what I do:

I added another JPannel and then added glue to it, then added content to it and then added the panel with glue to the frame in the scrollpane:

public static JPanel content;
public static JPanel wrapper;

public APP()
{
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setJMenuBar(new MenuBar());

    content = new JPanel(new GridLayout(0, 8, 2, 2));

    wrapper = new JPanel(new BorderLayout()); 
    wrapper.add(Box.createGlue(), BorderLayout.CENTER);
    wrapper.add(content, BorderLayout.NORTH);
    add(new JScrollPane(wrapper, 22, 32), BorderLayout.CENTER);

    pack();
    setLocationRelativeTo(null);
    setResizable(false);
    setVisible(true);
}

Then I just use wrapper.revalidate() so that it accounts for the new dimensions. Works like a charm now.

B.K.
  • 9,418
  • 9
  • 64
  • 98
  • I wander if it is a proper way to solve this kind of problem with BorderLayout and its BorderLayout.CENTER position. – polis Jun 15 '19 at 19:45