Transparent JButton

Rendicahya picture Rendicahya · Jan 3, 2011 · Viewed 90.4k times · Source

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.

Answer

camickr picture camickr · Jan 3, 2011
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);