Background color of JTextField doesn't become 'grayed out' when disabled after the background color had been changed before

喜夏-厌秋 提交于 2020-01-04 01:06:07

问题


Normally when you use setEditable(false) or setEnabled(false), the background/foreground color of the JTextField becomes 'grayed out'. However, if a background color had previously been set using setBackground(color) (for example to white), then the call to setEnabled or setEditable will not affect the background color anymore. Instead, it is overridden by the previously set color.

In WinForms (.NET) this is solved by "resetting" the background color to a non-overriding default value, namely Color.Empty. That would cause a text box to regain the standard behavior. However, I haven't found a similar "default value" for JTextField. How do I revert the JTextField to use the default colors and automatically switch the color when it is being disabled or set to read only? The foreground color has a similar issue.


回答1:


You need to reset the background color of the field to its default value.

The default UI delegate is looking for a UIResource in order to determine the correct shadings to use for the given field (based on the installed look and feel).

You can reset the background color using:

JTextField#setBackground(UIManager.getColor("TextField.background"))

Alternatively, you could construct a custom UIResource for your custom background.

Take a look at ColorUIResource for more details.




回答2:


How do I revert the JTextField to use the default colors

textField.setBackground( null );

automatically switch the color when it is being disabled or set to read only?

Use a PropertyChangeListener:

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class SSCCE extends JPanel implements PropertyChangeListener
{
    public SSCCE()
    {
        JTextField textField = new JTextField("Some Text");
        // Uncomment and run again to see the difference
        //textField.addPropertyChangeListener( this );
        textField.setBackground(Color.RED);
        textField.setEditable(false);
        add(textField);
    }

    public void propertyChange(PropertyChangeEvent e)
    {
        System.out.println(e.getPropertyName());
        JTextField textField = (JTextField)e.getSource();

        if ("editable".equals(e.getPropertyName()))
            textField.setBackground( null );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/15693497/background-color-of-jtextfield-doesnt-become-grayed-out-when-disabled-after-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!