How to remove JPanel padding in MigLayout?

The following situation: when I add JLabel to the panel, I get an unwanted padding / space. How can I remove it? Look at the left side, I want it to look like on the right.

enter image description here

here is my short test code that outputs the result shown on the left side of the image above:

setLayout(new MigLayout("gapy 0, debug")); JPanel line1 = new JPanel(); JPanel line2 = new JPanel();; line1.add(new JLabel("Text 1")); line2.add(new JLabel("Text 2")); add(line1, "wrap, align left"); add(line2); 
+2
java swing miglayout
source share
1 answer

This is because you are adding labels to the JPanel that used FlowLayout with default spaces. To fix this, you can use the following:

  JPanel line1 = new JPanel(new FlowLayout(FlowLayout.CENTER,0,0)); JPanel line2 = new JPanel(new BorderLayout()); 

enter image description here

+2
source share

All Articles