Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 863
dialer Avatar asked Dec 11 '22 16:12

dialer


2 Answers

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.

like image 51
MadProgrammer Avatar answered Apr 28 '23 08:04

MadProgrammer


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();
            }
        });
    }
}
like image 24
camickr Avatar answered Apr 28 '23 08:04

camickr