Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove space between components in MiGLayout

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: Space between components

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.

like image 537
Adam Smith Avatar asked Dec 20 '22 18:12

Adam Smith


1 Answers

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.

like image 69
Jacob Raihle Avatar answered Dec 24 '22 02:12

Jacob Raihle