Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 870
Rendicahya Avatar asked Jan 03 '11 15:01

Rendicahya


People also ask

How do I make a JButton transparent?

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.

How do I change the text color in a JButton?

To set the JButton text color, you can use setForeground .


2 Answers

button.setOpaque(false); button.setContentAreaFilled(false); button.setBorderPainted(false); 
like image 112
camickr Avatar answered Sep 21 '22 19:09

camickr


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);     } } 
like image 38
jjnguy Avatar answered Sep 21 '22 19:09

jjnguy