Question

Does somebody know how to remove white rectangle border on JButton ? This issue is there only for windows look & feel when the button is a little bit rounded.

Please find attached the example on the imageenter image description here.

Setting the border to empty or null doesn't help. The same for margin.

The white margin/border dissapear only when I set the opacity of the button to false, but unfortunately in this case also the whole button is opaque on some versions of windows.

When I set opacity to false, it looks like: enter image description here

Code example:

public class TestFrame extends javax.swing.JFrame {

/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {

            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }

            TestFrame inst = new TestFrame();
            inst.setLocationRelativeTo(null);
            inst.setVisible(true);
        }
    });
}

public TestFrame() {

    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    this.setLayout(null);
    this.getContentPane().setBackground(Color.BLACK);

    JButton button = new JButton();
    button.setBounds(10, 10, 100, 50);
    button.setBorder(BorderFactory.createEmptyBorder());    // not working
    button.setBorder(null);                                 // not working
    button.setMargin(new Insets(0,0,0,0));                  // not working

    add(button);
    pack();
    setSize(400, 300);
}

}

Thanks, Lubos

Was it helpful?

Solution

Seems like a painting problem. You can use:

button.setBackground( Color.BLACK );

OTHER TIPS

EDIT: See comments below. This sample still shows the effect, even using a proper layout. Setting the background color seems to show the unwanted border. This effect doesn't show with Metal. It seems as if Windows L&F shows a rounded edge, but the button is still rectangular. The space between is only noticable if the BG color of the container is changed to something obvious, like black.

import java.awt.*;

import javax.swing.*;

public class TestFrame extends JFrame
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        try
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }

        TestFrame inst = new TestFrame();
        inst.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        inst.setLocationRelativeTo(null);
        inst.setVisible(true);
      }
    });
  }

  public TestFrame()
  {
    JButton button = new JButton("One");
    JButton button2 = new JButton("Two");

    JPanel p = new JPanel();
    p.setBackground(Color.BLACK);
    p.setLayout(new FlowLayout());
    p.add(button);
    p.add(button2);

    add(p);
    setSize(400, 300);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top