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.
JButton can become transparentIf the value of the opaque property of a JButton is set to false, the background becomes transparent allowing whatever is behind the button to show through. Only the text and the border of the button remain opaque.
To set the JButton text color, you can use setForeground .
button.setOpaque(false); button.setContentAreaFilled(false); button.setBorderPainted(false);
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With