繁体   English   中英

如何更改JTextField的不可编辑前景

[英]How to change the non-editable foreground for JTextField

我想为我的应用程序中的所有文本字段更改不可编辑的前景(以避免JTextComponent.setEditable(false) ),但未能找到合适的属性密钥供UIManager执行。 是否可以全局更改不可编辑的前景?

在“正常”外观下,您可以使用"TextField.inactiveBackground"键,对于文本,您可以使用"TextField.inactiveForeground"

例如

UIManager.put("TextField.inactiveBackground", new ColorUIResource(Color.RED));

外观好像Nimbus可能需要一些“附加”工作...

NonEditableTextFied

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;

public class NonEdtiableField {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                UIManager.put("TextField.inactiveBackground", new ColorUIResource(Color.RED));

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                JTextField editable = new JTextField(10);
                JTextField nonEditable = new JTextField(10);
                nonEditable.setEditable(false);
                frame.add(editable);
                frame.add(nonEditable);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

找到解决方案:

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.synth.SynthTextFieldUI;

/**
 * <code>ExtSynthTextFieldUI</code>.
 *
 * @author SMedvynskyy
 */
public class ExtSynthTextFieldUI extends SynthTextFieldUI {

    /** Handler to change foreground when editable status changed. */
    private final PropertyChangeListener listener = new PropertyChangeListener() {

        /** Save the old color here. */
        private Color standardColor = UIManager.getColor(getPropertyPrefix() + ".foreground");

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ("editable".equals(evt.getPropertyName())) {
                if (Boolean.TRUE.equals(evt.getNewValue())) {
                    getComponent().setForeground(standardColor);
                } else {
                    standardColor = getComponent().getForeground();
                    // set the "inactive foreground"
                    getComponent().setForeground(new ColorUIResource(0xA0A0A0));
                }
            }
        }
    };

    /**
     * Creates a new UI object for the given component.
     *
     * @param table component to create UI object for
     * @return the UI object
     */
    public static ComponentUI createUI(JComponent table) {
        return new ExtSynthTextFieldUI();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void installListeners() {
        super.installListeners();
        getComponent().addPropertyChangeListener("editable", listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void uninstallListeners() {
        getComponent().removePropertyChangeListener("editable", listener);
        super.uninstallListeners();
    }
}

安装L&F之后,我只需要重置默认UI:

UIManager.put("TextFieldUI", ExtSynthTextFieldUI.class.getName());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM