Change Color SetEnabled

user1387989 picture user1387989 · May 15, 2012 · Viewed 9.7k times · Source

My question is this: Default change the color to give a JTextField setEnabled (false) for example is black and proven with UIManager results but have not had any suggestions.

Greetings.

Answer

tenorsax picture tenorsax · May 15, 2012

To change disabled background color try this:

UIManager.put("TextField.disabledBackground", Color.GRAY);

You can change disabled text color using setDisabledTextColor, i.e.:

textField.setDisabledTextColor(Color.GRAY);

EDIT: include SSCCE

import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class TestDisabledTextField {

    public static void main(String[] args) {
        UIManager.put("TextField.disabledBackground", Color.YELLOW);

        JTextField textField = new JTextField("Disabled text field", 15);
        textField.setEnabled(false);
        textField.setDisabledTextColor(Color.RED);

        JPanel panel = new JPanel();
        panel.add(textField);

        JOptionPane.showMessageDialog(null, panel);
    }
}