Question

I have a small "popup" like this:

Tiny button

But I don't want the padding around the button, I want it to be as small as it can be with the supplied text.

btn.setMargin(new Insets(1, 1, 1, 1));
panel.add(lbl, "wrap");
panel.add(btn);
frame.pack();
frame.setVisible(true);

At least this doesn't work... Can this be achieved on MigLayout or should I use some other layout manager for this frame.

Was it helpful?

Solution

A layout manager should not affect the way a component is painted.

The setMargin(...) works fine for me when using the Metal LAF. Maybe your LAF doesn't support the setMargin() method. Or maybe you have some code elsewhere that is overriding the preferred size of the button causing it to paint like this.

However, if the MIG layout turns out to be the problem, you could try the Wrap Layout.

OTHER TIPS

What's your MigLayout constraints that it was built with? You might want to try using novisualpadding if you aren't already. Also, the debug layout constraint will draw boxes around your components to show what room they take up. (I think the red dotted lines are the size of the components and the blue dotted lines are the "cells" that the component is in.)

// old version
// JPanel panel = new JPanel(new MigLayout());

// new verion
JPanel panel = new JPanel(new MigLayout("debug, novisualpadding"));

I use MigLayout pretty often and have found the "cheat sheet" pretty useful. http://migcalendar.com/miglayout/cheatsheet.html

Taking MIke's answer as a startup, I found in the MIG "CheatSheet" a possible solution to your problem.

When you set up a MIGLayout, you can specify the gap between component with an integer between the square brackets defining columns and rows.

E.g.

MigLayout buttonMig = new MigLayout("", "[139px,grow]0[179px,grow]50[142px,grow][143px,grow]", "0[60px:60px:60px,center]");

I've just been having this issue too. I discovered that the following worked for me:

this.add(okButton, "width pref:pref:pref");

or

this.add(okButton, "wmax pref");

Also when using the substance look and feel there is a feature where the minimum size is always too wide. This can be overridden with this obscure hack.

okButton.putClientProperty(
    SubstanceLookAndFeel.BUTTON_NO_MIN_SIZE_PROPERTY, Boolean.TRUE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top