5

What I want is to make the background of a uneditable text area the same as its disabled background.

I know that the color is available from the UIManager with the key TextArea.disabled:

DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223

I firstly tried:

textArea.setBackground(UIManager.getColor("TextArea.disabled"));

It changed nothing at all and the background was still white.

Then I tried:

textArea.setBackground(new Color(UIManager.getColor("TextArea.disabled").getRGB()));

The background did change but was not exactly the same as the disabled background which looks brighter.

What is the correct way to deal with this derived color?

Zhao Yi
  • 2,335
  • 7
  • 23
  • 27

2 Answers2

4

@Zhao Yi wrote There is no key for uneditable background

enter image description here

code for Java6, have to change imports for Java7

import com.sun.java.swing.Painter;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class TestNimbusUIManagerTextArea {

    private static JFrame frame = new JFrame("Nimbus UIDeafaults");
    private JTextArea testEnableTextArea = new JTextArea("enabled JTextArea");
    private JTextArea testDisableTextArea = new JTextArea("disabled JTextArea");

    public TestNimbusUIManagerTextArea() {
        testDisableTextArea.setEnabled(false);
        frame.setLayout(new GridLayout(2, 0, 20, 20));
        frame.add(testEnableTextArea);
        frame.add(testDisableTextArea);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 105);
        frame.pack();
        frame.setVisible(true);
    }

    private static void customizeNimbusLaF() {
        UIManager.getLookAndFeelDefaults().put(
                "TextArea[Enabled+NotInScrollPane].backgroundPainter",
                new FillPainter(new Color(127, 255, 191)));
        UIManager.getLookAndFeelDefaults().put(
                "TextArea[Disabled+NotInScrollPane].backgroundPainter",
                new FillPainter(new Color(127, 255, 191)));
        SwingUtilities.updateComponentTreeUI(frame);
    }

    public static void main(String arg[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    customizeNimbusLaF();                   
                    break;
                }
            }
        } catch (Exception e) {
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestNimbusUIManagerTextArea tNUIM = new TestNimbusUIManagerTextArea();
            }
        });
    }
}

class FillPainter implements Painter<JComponent> {

    private final Color color;

    FillPainter(Color c) {
        color = c;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        g.setColor(color);
        g.fillRect(0, 0, width - 1, height - 1);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for the code, but this is not what I want. I just want to make the uneditable (not disabled) text areas have the same background as its disabled background (Nimbus default). Additionally, `Painter` is not a public API, so it's not a good practice to customized Nimbus like this. – Zhao Yi Jan 11 '13 at 02:04
  • in the case that you JTextArea.setEditable(false), then nothing happened, Colors scheme stays unchanged, use (not for Nimbus) that for JTextComponent only – mKorbel Jan 11 '13 at 07:49
  • +1, Nimbus is more complicated than other LAF. It looks like I need to update the UIManagerDefaults to include the new Nimbus syntax for properties. – camickr Jun 18 '13 at 16:00
0

I've found the answer. The color used for disabled background is not UIManager.getColor("TextArea.disabled"), but hard-coded in class TextAreaPainter:

private Color color1 = decodeColor("nimbusBlueGrey", -0.015872955f, -0.07995863f, 0.15294117f, 0);

Using this color solves my issue.

Zhao Yi
  • 2,335
  • 7
  • 23
  • 27