Question

I'm writing a calendar panel to use in an application. To display days I use JButtons. Since I don't want the buttons to take a lot of space they have no insets.

Here is how it looks in my GTK+:
enter image description here
All buttons are displayed nice and clean.

Now here is how things look when I switch to the Metal laf:
enter image description here
Simply said, this isn't acceptable.

When I set the insets by putting setMargins(new insets(0,0,0,0)) in the constructor of the custom button all works and it looks like I want it to look.
enter image description here

Now those buttons aren't the only buttons affected in a negative way and I don't feel like I should set this everywhere manually so I need a cleaner solution. I've tried setting the default button margin through UIManager.put("Button.margin", new Insets(0, 0, 0, 0)); but this didn't affect anything. This is driving me nuts, and I don't know how this affects other lafs like Aqua or Windows laf but I fear the worst.

Edit: Just tested it in Windows XP and guess what:
enter image description here

Edit2:
The Main() creates and shows this frame. It has several components on it who are created by initComponents(). The initComponents() was created by Netbeans and it has a pack() at the end.

public class MainForm
    extends javax.swing.JFrame
{
    public MainForm()
    {
        String os = System.getProperty("os.name").toLowerCase();
        String laf = null;
        if (os.contains("win"))
        {
            laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
        }
        else if (os.contains("nix") || os.contains("nux"))
        {
            laf = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
        }
        else if (os.contains("mac"))
        {
            laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        }
        try
        {
            UIManager.put("Button.margin", new Insets(0, 0, 0, 0));
            UIManager.setLookAndFeel(laf);
            SwingUtilities.updateComponentTreeUI(this);
            System.out.println(UIManager.getLookAndFeel().toString());
        }
        catch (Exception ex)
        {
            System.out.println(ex.toString());
        }
        initComponents();
    }
}
Was it helpful?

Solution

All right I solved my issue and of course it was actually quite simple, here it comes.
The buttons I use in my calendar panel are JToggleButtons. The "Button.margin" only applies to JButtons. So all I needed to do was to add 1 line:
UIManager.put("ToggleButton.margin", new Insets(0, 0, 0, 0));

A list of items you can set in the UIManager can be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top