sokol1412
sokol1412

Reputation: 9

Swing MigLayout - can't avoid unnecessary gap

I'm writing some Swing application for surveying purposes using MigLayout. I'm trying to remove unnecessary gap.

I've already noticed that the gap is present because of the sentence at the top. When I remove it, however, it looks fine.

The issue is visible on the following picture:

enter image description here

The way I add the sentence is:

add(text, "wrap, center, gap top 20");

And the way I add buttons:

String wrap = shouldWrap(i) ? "wrap" : "";
this.add(button, "gapbefore 15, width 50, height 20, " + wrap);

Can anyone please help me with explaining why it's happening and how I can remove the gap with the sentence present?

////////////EDIT

I wanted to work with my current infrastucture, I've enabled debug mode in MigLayout and I see something like this:

My JPanel in debug mode looks like this

I'm wondering why border's size around every first component in a row is set to match the "Please, specify how much you agree with the given statements" sentence's size.

Is there any way to disable that option for every component using MigLayout?

Upvotes: 0

Views: 273

Answers (2)

hamena314
hamena314

Reputation: 3109

You simply can use the constraing "split x", which will divide the row into X parts that are huddled together:

Without split:

public class SentenceExample {

    public static void main(String[] args) {
        JFrame frame = new JFrame("JFrame Example");
        JPanel panel = new JPanel();

        panel.setLayout(new MigLayout());

        panel.add(new JLabel("Please, specify how much you agree with the given statements"), "wrap");

        panel.add(new JButton("1"));
        panel.add(new JButton("2"));
        panel.add(new JButton("3"));
        panel.add(new JButton("4"));
        panel.add(new JButton("5"));
        panel.add(new JButton("6"));
        panel.add(new JButton("7"));
        panel.add(new JButton("8"));
        panel.add(new JButton("9"));
        panel.add(new JButton("10"));

        frame.add(panel);
        frame.setSize(900, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

This looks like this:

WithoutSplit

With split:

If you change the first JButton-line from this:

panel.add(new JButton("1"));

to this:

panel.add(new JButton("1"), "split 10");

your GUI will look like this:

WithSplit

The first button with the constraint "split 10" arranges 10 "slots", where the button itself and the next 9 elements will be put into.

Do this for the first button in every row (i.e. with a loop) and you have the look you wanted.


Edit: If using a loop for every first button is too cumbersome, you can use the column constraints for the panel-layout itself, by making it have i.e. 7 columns and make the sentence span over several columns. See here http://www.miglayout.com/QuickStart.pdf for how to create column constraints.

"split" might be easier to use, but the column constraints might be a bit cleaner.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Why not simply nest JPanels and use a compound layout?

For instance, the overall JPanel could use a BorderLayout with the JLabel title at the top (BorderLayout.PAGE_START possition), and an inner JPanel could use MigLayout, hold the grid of components, and be held in the outer JPanel's BorderLayout.CENTER position.

Upvotes: 0

Related Questions