Search code examples
javaswinglook-and-feelnimbus

Swing Nimbus change style of a non editable JTextField


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


Solution

  • 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

    enter image description here

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