Question

I have three JPanels and I add them to a contentPane with gridlayout; the gridlayout contains 3 rows. I have annoying gaps between the panels. How can I get rid of these easily? I tried resizing my JFrame, but the spaces are still there. I also tried readjusting the gridlayout options such as HGap and Vgap, but it didn't fix the problem. I looked at some previous questions about this, but none of them solved the problem. It looks like this:

----JPanel 1 here------





----JPanel 2 here-----





----Jpanel 3 here-----
Was it helpful?

Solution

When set the layout manage with a GridLayout, you can specify the gaps between elements:

setLayout(new GridLayout(rows, cols, hgap, vgap));

hgap > the gap size between elements horizontally

vgap > the gap size between elements vertically

Java doc:

In addition, the horizontal and vertical gaps are set to the specified values. Horizontal gaps are placed between each of the columns. Vertical gaps are placed between each of the rows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top