1

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);
Noel
  • 23
  • 4
  • 1
    the issue that your facing is look and feel related , that is if you really didn't add another piece of code to listen to the click and change the color out side your MVCE , any how what look and feel are you using ? if it turned to light blue the i can assume it's either windows (focus state) or nimbus(model armed state), try to change the look and feel and if it still changing please tell us what laf you are using . – OverLoadedBurden May 28 '19 at 02:41
  • 1
    Possible duplicate of [How to change a JButton color on mouse pressed?](https://stackoverflow.com/questions/14627223/how-to-change-a-jbutton-color-on-mouse-pressed) – Maifee Ul Asad May 28 '19 at 05:11
  • You can simply override and get the transparent look you wanted. ^ question contains your answer. – Maifee Ul Asad May 28 '19 at 05:12

1 Answers1

1
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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I tried that but on button press the background of the button gets distorted. – Noel May 29 '19 at 12:46
  • It gets distorted because you are using a transparent color. If you need more help post your [mre] demonstrating the problem That is all you need is a JFrame with a single JButton and you set the above properties to test my suggestion. – camickr May 29 '19 at 15:10