How to remove space between components in MiGLayout

I have not used MiGLayout after a while, and I donโ€™t remember how to remove the space that is automatically set between the components. I tried to use the following parameters to no avail (note that I want to keep the horizontal distance):

novisualpadding

pad 0

insert 0

growy

Here is an example of what I mean: Space between components

I would like the lines to be grouped in two. Thus, the first and second lines of JTextFields should not have a gap between them. However, I want to keep the gap between the second and third row. I want the third and fourth lines to be grouped without spaces between them, etc.

Here is the relevant part of my code for the layout (this is in a class that extends JPanel):

setLayout(new MigLayout("insets 0", "grow")); //Code to create the JTextFields not shown as it is not relevant for(int i = 0; i < textFields.length; ++i) { for(int j = 0; j < textFields[0].length; ++j) { textFields[i][j].setPreferredSize(new Dimension(80, 50)); if(j == 0 && i % 2 == 0) //If it the first JTextField in an even row add(textFields[i][j], "newline, split, gaptop 5, gapright 5"); else if(j == 0 && i % 2 != 0) //If it the first JTextField in an odd row add(textFields[i][j], "newline, split, gapright 5"); else //if it any other JTextField add(textFields[i][j], "gapright 5"); } } 

Basically, I use loops to traverse all my components, then I set a space above the odd lines, because where I want a space between the lines in my components and for other components, I set the same parameters except for this space.

In the end, I'm going to group all the JTextFields from the same row in the JPanel and add JPanels to the layout instead, but that doesn't matter at the moment.

+4
source share
2 answers

You must remove it in LayoutConstraints, spaces in ComponentConstraints can only increase the default values, but not decrease. And remember: never use setXXSize for a component :-) Instead, if you really want hard-coded sizes, do it in LayoutManger and think about it in the screen resolution in an independent manner - you have a very strong beast in your hands. Plus: do-not-repeat-yourself is done for layouts, as well as for all other parts of the code. It is best to determine as much as possible in the layer with the highest level of restrictions.

Some snippets of code (used by SwingX JXPanel to simply set the background image)

 int rows = 10; int columns = 20; MigLayout layout = new MigLayout( // set the automatic wrap after columns "insets 0, wrap " + columns, // hardcode fixed column width and fixed column gap "[50lp, fill]5lp", // hardcode fixed height and a zero row gap "[20lp, fill]0"); JXPanel content = new JXPanel(layout); content.setBackgroundPainter(new ImagePainter(XTestUtils.loadDefaultImage("moon.jpg"))); for (int r = 0; r < rows; r++) { // top gap on even rows String topGap = r != 0 && r % 2 == 0 ? "gaptop 5lp" : ""; for (int i = 0; i < columns; i++) { JTextField field = new JTextField(); content.add(field, topGap); } } showInFrame(content, "grid"); 

just looked at the last sentence:

In the end, I'm going to group all the JTextFields from one row in a JPanel and add JPanels to the layout instead.

believe that this is not so: nested panels are a kludge for low-power layouts, MigLayout is designed for an all-in-one approach (or at least larger ones).

Sorry for the many do-nots :-)

+5
source

You must explicitly set the width gap to 0, because by default it is a platform-specific โ€œboundโ€ gap. You can do this at the layout level or row / column level. eg:.

 setLayout(new MigLayout("gap rel 0", "grow")); 

Then you can use existing restrictions for odd lines.

+6
source

All Articles