SL_User
SL_User

Reputation: 1954

How to disable JButton without hiding its label?

I'm developing a project in Java using netbeans IDE and I need to disable a particular JButton. I use the following code for that.

IssuBtn.setEnabled(false);

But after it is disabled it doesn't show the text on the JButton. How can I keep that text on the JButton?

Upvotes: 3

Views: 7290

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168845

This experiment suggests one answer is 'Use a PLAF that is not Metal'.

Look Of Disabled Buttons

import java.awt.*;
import javax.swing.*;

class LookOfDisabledButton {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2));
                pEnabled.setBackground(Color.green);
                gui.add(pEnabled, BorderLayout.NORTH);

                JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2));
                pDisabled.setBackground(Color.red);
                gui.add(pDisabled, BorderLayout.SOUTH);

                UIManager.LookAndFeelInfo[] plafs = 
                    UIManager.getInstalledLookAndFeels();
                for (UIManager.LookAndFeelInfo plafInfo : plafs) {
                    try {
                        UIManager.setLookAndFeel(plafInfo.getClassName());
                        JButton bEnabled = new JButton(plafInfo.getName());
                        pEnabled.add(bEnabled);
                        JButton bDisabled = new JButton(plafInfo.getName());
                        bDisabled.setEnabled(false);
                        pDisabled.add(bDisabled);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

Alternately, adjust the values in the UIManager.

UIManager tweak

import java.awt.*;
import javax.swing.*;

class LookOfDisabledButton {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));
                JPanel pEnabled = new JPanel(new GridLayout(1,0,2,2));
                pEnabled.setBackground(Color.green);
                gui.add(pEnabled, BorderLayout.NORTH);

                JPanel pDisabled = new JPanel(new GridLayout(1,0,2,2));
                pDisabled.setBackground(Color.red);
                gui.add(pDisabled, BorderLayout.SOUTH);

                // tweak the Color of the Metal disabled button
                UIManager.put("Button.disabledText", new Color(40,40,255));

                UIManager.LookAndFeelInfo[] plafs = 
                    UIManager.getInstalledLookAndFeels();
                for (UIManager.LookAndFeelInfo plafInfo : plafs) {
                    try {
                        UIManager.setLookAndFeel(plafInfo.getClassName());
                        JButton bEnabled = new JButton(plafInfo.getName());
                        pEnabled.add(bEnabled);
                        JButton bDisabled = new JButton(plafInfo.getName());
                        bDisabled.setEnabled(false);
                        pDisabled.add(bDisabled);
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }

                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

As pointed out by kleopatra..

it's not a solution but might be a pointer to the direction to search for a solution

Where 'it' is my answer. In fact, I suspect she hit upon the real cause with the comment:

guessing only: here it's due to violating the one-plaf-only rule.

I second that guess.

Upvotes: 12

Related Questions