3

I'm trying to use Nimbus Look and Feel and I can't simply plug it in to replace other Look and feel because it adds some external padding around each component.

So, I would like to remove this padding. This is the list of defaults that can be changed, however the following code changes nothing.

UIManager.put("Button.contentMargins", new Insets(0, 0, 0, 0));

Other values are working as expected.

How can I remove this padding?

EDIT

I believe content margins is referring to the internal padding.

EDIT

Looking at the source code the external padding seems to be hard-coded. I believe it's added to allow for the focus selection property.

Should this be considered a bug? No other L&F does this, and with this extra padding it's not possible to reuse the same layout (as previous layouts all will be assuming no padding).

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Pool
  • 11,999
  • 14
  • 68
  • 78

2 Answers2

4

I found that you have to set those defaults right after you set the look and feel (i.e., before you start creating any UI components):

try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    UIManager.getLookAndFeelDefaults().put("Table.cellNoFocusBorder", new Insets(0,0,0,0));
    UIManager.getLookAndFeelDefaults().put("Table.focusCellHighlightBorder", new Insets(0,0,0,0));
    Insets buttonInsets = new Insets(3, 6, 3, 6);
    UIManager.getLookAndFeelDefaults().put("Button.contentMargins", buttonInsets);
}
catch (Exception e) {
    e.printStackTrace();
}

Also note that you need to set them on the "LookAndFeelDefaults" not the "developer" defaults.

0

Force nimbus to create the defaults first by calling getDefaults():

NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
UIDefaults def = laf.getDefaults();
def.put("Button.contentMargins", new Insets(0,0,0,0));
def.put("DesktopIcon.contentMargins", new Insets(0,0,0,0));
def.put("Label.contentMargins", new Insets(0,0,0,0));

UIManager.put() won't store the settings in the nimbus defaults if the defaults was not initialized first.

arnon
  • 61
  • 1
  • 1