Swing Nimbus change style of a non editable JTextField

爷,独闯天下 提交于 2019-12-11 11:54:16

问题


Is there a way to change the style of non editable JTextField? We would like to give the TextField a slightly more grayish background if it is set to editable = false. I guess one way would be

readonlyComponent.setBackground(Color.LIGHT_GRAY);

on every component but this seems error prone and tedious.

What I want to achieve is this: Normal JTextFiels should appear as defined by the nimbus look and feel. Readonly fields, e.g. editable = false, should have a different background color. Disabled fields should appear as defined by the nimbus LAF.

I couldn't find an entry in the Nimbus style list


回答1:


I couldn't find an entry in the Nimbus style list

  • in this case is there way, keys are accesible from standard code, note most of keys aren't accesible in Java7(changes from sun.com to java.swing), f.i. more than half methods for JLabel etc.

  • another way is by override primary and secondary Colors

  • to test SeaGlass L&F (based on Nimbus), maybe all key are fixed and is possible to set Colors without hacks

from code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Painter;
import javax.swing.UIManager;

public class NimbusTest3 {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private JButton button = new JButton("Text");
    private JTextField label = new JTextField("Text");
    private JTextField label1 = new JTextField("Text");

    public NimbusTest3() {
        label.setEnabled(false);
        frame.add(button);
        frame.add(label, BorderLayout.NORTH);
        frame.add(label1, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("TextField[Disabled].backgroundPainter",
                            new FillPainterUI(new Color(127, 255, 191)));
                    UIManager.getLookAndFeelDefaults().put("TextField[Disabled].textForeground", new Color(255, 0, 0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                NimbusTest3 nimbusTest3 = new NimbusTest3();
            }
        });
    }

    static class FillPainterUI implements Painter<JComponent> {
        // fills whole, available area, ignoring rounded corners, Borders e.i.
        private final Color color;

        FillPainterUI(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);
        }
    }
}



回答2:


If anyone still intersted in setting the bgcolor only for non editable JTextField:

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("TextField[Enabled].backgroundPainter", new NimbusTextFieldBgPainterUI(
  (Color) defaults.get("TextField.disabled"), 
  (Color) defaults.get("TextField.background")));
...
static class NimbusTextFieldBgPainterUI implements Painter<JComponent> {
        private final Color nonEditableBgColor;
        private final Color editableBgColor;

        NimbusTextFieldBgPainterUI(Color cNonEditable, Color cEditable) {
            this.nonEditableBgColor = cNonEditable;
            this.editableBgColor = cEditable;
        }

        @Override
        public void paint(Graphics2D g, JComponent object, int width, int height) {
            Color c;
            if (object instanceof JTextField && !((JTextField) object).isEditable()) {
                c = nonEditableBgColor;
            } else {
                c = editableBgColor;
            }
            g.setColor(c);
            g.fillRect(0, 0, width - 1, height - 1);
        }
    }


来源:https://stackoverflow.com/questions/22834531/swing-nimbus-change-style-of-a-non-editable-jtextfield

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