Reputation: 4944
I haven't used MiGLayout in a while and I can't remember how to remove the space that gets put in automatically between components. I tried using the following parameters to no avail (note that I want to keep the horizontal spacing):
novisualpadding
pad 0
insets 0
growy
Here is an example of what I mean:
What I'd want is to have the rows grouped two by two. So the first and second JTextFields rows shouldn't have a gap between them. I want to keep the gap between the second and third row, though. I want the third and fourth row grouped without any gaps 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's 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's the first JTextField in an odd row
add(textFields[i][j], "newline, split, gapright 5");
else //if it's any other JTextField
add(textFields[i][j], "gapright 5");
}
}
So basically, I use loops to go through all my components, I then set a gap above odd rows because that's where I want space between rows in my components, and for other components, I set the same parameters except for that gap.
Eventually, I'm going to group all the JTextFields from the same line in a JPanel and add the JPanels to the layout instead, but it's not what matters at the moment.
Upvotes: 4
Views: 7948
Reputation: 51525
You have to remove it in the LayoutConstraints, gaps in ComponentConstraints can only increase the defaults, but not decrease. And remember: never-ever use setXXSize on a component :-) Instead, if you really want hardcoded sizes, do so in the LayoutManger and consider doing it in a screen resolution independent manner - you have a very powerful beast in your hands. Plus: do-not-repeat-yourself holds for layouts as well as for all other code parts. Best to define as much as possible in the highest constraint layer.
Some code snippet (used SwingX JXPanel just to easily set a 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 saw your last sentence:
Eventually, I'm going to group all the JTextFields from the same line in a JPanel and add the JPanels to the layout instead,
consider not doing so: nesting panels is a kludge for not-powerful-enough layoutManagers, MigLayout is designed for an all-in-one (or at least bigger portions) approach.
Sorry for the many do-nots :-)
Upvotes: 5
Reputation: 3750
You have to explicitly set a 0-width gap, because the default is the platform-dependent "related" gap. You can do this on a layout level or row/column level. e.g.:
setLayout(new MigLayout("gap rel 0", "grow"));
You can then use your existing constraints for the odd rows.
Upvotes: 7