0

I'm working on a game that disables JToggleButton buttons when I click on them, but I want the buttons to also change color when they're disabled. I disable a button with:

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jToggleButton1.setEnabled(false);
}

I've tried using jToggleButton1.setBackground(Color.RED); before and after the jToggleButton1.setEnabled(false); but the button always turns gray anyway when it's disabled.

Does anybody know a solution?

Marco
  • 11
  • 2
  • try to look at this. https://stackoverflow.com/questions/5808022/changing-the-background-color-of-a-selected-jtogglebutton – Jam Oct 07 '19 at 05:51
  • What's wrong with Gray? Pretty much the standard for a disabled control. Don't disable it then, In the **ActionPerformed** event: `jToggleButton1.setBackground(Color.red); jToggleButton1.setForeground(Color.white); If (jToggleButton1.getBackColor() == Color.red) { return; }` – DevilsHnd - 退職した Oct 07 '19 at 06:01
  • The problem is that the standard UI of a button, provides no support for color change, when button is disabled. So it's possible, but requires deep Swing knowledge (you need to create your own implementation of ButtonUI based on your current Look-n-Feel). – Sergiy Medvynskyy Oct 07 '19 at 06:23
  • jToggleButton1.addChangeListener(changeEvent -> jToggleButton1.setBackground(jTB1.isEnabled() ? ... : ...));` – Joop Eggen Oct 07 '19 at 11:32

0 Answers0