Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JButton's border don't go away even when set the Border to EmptyBorder

I am designing a GUI using Eclipse on Mac, and I want to make the JButton to display only the Icon I set. It looks fine on Mac OS, but when I export it to a jar file, and rut in on Windows OS, the JButton looks like this:

enter image description here

The borders are all EmptyBorder.

What did I do wrong? How can I make the square at the back go away?

like image 880
xiaofan2406 Avatar asked Dec 17 '22 08:12

xiaofan2406


2 Answers

To answer this question correctly, an SSCCE will most likely be required. Anyway, I believe that you'll want to invoke setContentAreaFilled(false) on your JButton instances. That should effectively remove the "square".

However, it is important to note that the exact behavior of calling this function varies on a component-by-component and L&F-by-L&F basis.


import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class JButtonDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JIconButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JIconButton extends JButton{
        private static final long serialVersionUID = 7274140930080397481L;

        public JIconButton(){
            super(UIManager.getIcon("OptionPane.informationIcon"));
            setContentAreaFilled(false);
            setFocusPainted(false);
            setBorder(BorderFactory.createEmptyBorder());
        }
    }
}

enter image description here

like image 137
mre Avatar answered Dec 18 '22 22:12

mre


1) can't to reproduce that, too hard to say without seeing code that you wrote for that,

2) because you touched XxxButtonUI, and this is really Native OS rellated issue, which Look and Feel is there used for that

3) what happens if you override MetalButtonUI?

4) is there transulcent background, or with some Color, or ...

like image 44
mKorbel Avatar answered Dec 18 '22 22:12

mKorbel