Search code examples
javaswingwindows-7look-and-feelinsets

Remove JButton rectangle white border


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


Solution

  • Seems like a painting problem. You can use:

    button.setBackground( Color.BLACK );