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

时间:2014-10-08 11:14:19

标签: java swing jtextfield look-and-feel

我想更改应用程序中所有文本字段的不可编辑前景(以避免不可理解性JTextComponent.setEditable(false)),但未能找到UIManager执行此操作的相应属性键。是否可以全局更改不可编辑的前景?

2 个答案:

答案 0 :(得分:2)

"正常"外观和感觉,您可以使用"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);
            }
        });
    }

}

答案 1 :(得分:2)

找到解决方案:

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&amp; F后,我只需要重置默认用户界面:

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