Transparent JButton

Is it possible to make a JButton transparent (including the border) but not the text? I extend swing's JButton and override this:

@Override
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
    super.paint(g2);
    g2.dispose();
}

but it makes everything transparent, including the text. Thanks.


Solution 1:

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

Solution 2:

The following should do the trick.

public class PlainJButton extends JButton {

    public PlainJButton (String text){
        super(text);
        setBorder(null);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setOpaque(false);
    }

    // sample test method
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(new PlainJButton("HI!!!!"));
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
    }
}