Reputation: 23
I am trying to make transparent JButtons (with visible text) but when the button has clicked the background becomes light blue. (Don't mind the indentation in the code. It's all properly indented)
I've made the button successfully transparent but I fear that the problem might be because I add all the JButtons to a JLabel(A background image).
JButton play = new JButton("Play");
JButton quit = new JButton("Quit");
JButton instructions = new JButton("Instructions");
Color invs = new Color(0,0,0,0);
play.setBackground(invs);
quit.setBackground(invs);
instructions.setBackground(invs);
play.setBorderPainted(false);
//play.setMargin(new Insets(0,0,0,0));
play.setRolloverEnabled(false);
play.setFocusable(false);
play.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
instructions.setBorderPainted(false);
//instructions.setMargin(new Insets(0,0,0,0));
instructions.setRolloverEnabled(false);
instructions.setFocusable(false);
instructions.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
quit.setBorderPainted(false);
//quit.setMargin(new Insets(0,0,0,0));
quit.setRolloverEnabled(false);
quit.setFocusable(false);
quit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
instructions.setForeground(Color.WHITE);
play.setForeground(Color.WHITE);
quit.setForeground(Color.WHITE);
Upvotes: 1
Views: 705
Reputation: 324118
Color invs = new Color(0,0,0,0);
Don't use a transparent Color to attempt to set the background of any Swing component. Swing does not paint components correctly when using transparent colors.
In general you use:
setOpaque( false );
when you want full transparency on any Swing component.
However with a JButton you also need:
setContentAreaFilled( false );
to prevent the button background from being painted when it is clicked.
If you ever want partial transparency, then check out Backgrounds With Transparency for a solution.
Upvotes: 1