Skip to main content

APIzation: Replication Package

[Q#36652457][A#36652931] How to stop text going grey when disabling a button?

https://stackoverflow.com/q/36652457

My current java version is: I'm very new to swing and trying to write my first minesweeper app. Now, I need to disable a button when clicking on it and print a number of adjacent mines (with the appropriate text color depending on a number: 1- blue, 2 - green, 3 - red, etc). So as suggested in this answer I wrote the following: But it still goes grey when disabling:

But enabled button look just as I want it to:

What to do with this issue? How to fix this problem with disabling buttons?

Answer

https://stackoverflow.com/a/36652931

Maybe you know some workarounds Use Icons instead of text. Then you can use:

APIzation

JButton button = new JButton( icon3 );
button.setDisabledIcon( icon3 );
package com.stackoverflow.api;

import javax.swing.*;

public class Human36652931 {

  public static JButton createJButtonWithDisabledIcon(Icon icon3) {
    JButton button = new JButton(icon3);
    button.setDisabledIcon(icon3);
    return button;
  }
}

package com.stackoverflow.api;

import javax.swing.Icon;
import javax.swing.JButton;

/**
 * How to stop text going grey when disabling a button?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/36652931">https://stackoverflow.com/a/36652931</a>
 */
public class APIzator36652931 {

  public static void stop(Icon icon3) throws Exception {
    JButton button = new JButton(icon3);
    button.setDisabledIcon(icon3);
  }
}