JButton margins. Not respected when nimbus plaf
Asked Answered
F

2

3

The property margin of a JButton isn't respected when the nimbus look and feel is installed.
. I need some "little" buttons, but nimbus forces the space around button text to be large, so I only get "very large" buttons.
I discovered in nimbus defaults page that there is a property called:

Button.contentMargins

that is preset with large values.
I've tryed to override it with the following code:

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

in the main, just after setting the nimbus look and feel.

But nothing happens, the empty space around buttons text still remains large. Any idea?

Farseeing answered 27/11, 2011 at 18:56 Comment(0)
A
3

Altering the value of the JComponent.sizeVariant may also be effective, as discussed in Resizing a Component.

Autunite answered 27/11, 2011 at 22:21 Comment(0)
F
2

on base of thread How to change the background color for JPanels with Nimbus Look and Feel? is possible to change and assign one value for something from Nimbus Defaults,

but are you sure that you needed this output to the GUI, nothing nice

enter image description here

v.s. basic JButton with Nimbus L&F

enter image description here

from code

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.InsetsUIResource;

public class NimbusJPanelBackGround {

    public NimbusJPanelBackGround() {
        JButton btn = new JButton("  Whatever  ");
        JButton btn1 = new JButton("  Whatever  ");
        JPanel p = new JPanel();
        p.add(btn);
        p.add(btn1);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.add(p, BorderLayout.CENTER);
        f.setSize(200, 100);
        f.setLocation(150, 150);
        f.setVisible(true);
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
                    UIManager.getLookAndFeelDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround();
            }
        });
    }
}

previously +1 for interesting question

Fender answered 27/11, 2011 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.