Pregunta

I had a JTextPane had a differen background and foreground color.Now when the L&F is changed to Nimbus L&F, my JTextPane 's color is changed.How come?Only this class is having such problem.While others works well.What is the problem?

This is how i change L&F :

for (javax.swing.UIManager.LookAndFeelInfo info : 

javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PlayBackVoice.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

On the other hand, the color changed very well before setting this L&F. Or these statements were working well:

     jtp.setBackground(Color.BLACK);
     jtp.setForeground(Color.WHITE);

Any idea what is wrong?

¿Fue útil?

Solución

Swing components delegate their look and feel to ComponentUI objects. As part of Swing, there are interfaces defined for each component: ButtonUI which JButton delegates to, LabelUI for JLabel, TextUI for JTextPane, etc.

Each Swing look and feel contains implementations for each of those interfaces. eg. MetalButtonUI, MetalLabelUI, etc. which paint that component however the look and feel wants to.

When you call UIManager.setLookAndFeel it swaps in that set of implementations.

All very clever, but the annoying thing is that each look and feel doesn't have to honour any of your foreground / background / border, etc. settings.

Luckily, Nimbus defines all of its colours as UIManager keys.

So, you can do this sort of thing to override its default colours:

UIManager.put("nimbusBase", Color.BLACK);

See here for full list:

http://www.ce.unipr.it/people/poggi/teaching/docs/javaSE7.0Tutorial/uiswing/lookandfeel/_nimbusDefaults.html

Update

Although, saying that, it doesn't look like Nimbus plays nicely at all! Some people have had some luck overriding Nimbus colours with this:

Color bgColor = new Color("99999");
UIDefaults defaults = new UIDefaults();
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
jeditorpane.putClientProperty("Nimbus.Overrides", defaults);
jeditorpane.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
jeditorpane.setBackground(bgColor);

Otros consejos

This is slight update to Mr Spoon's update. Actually the code

defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);

is wrong as the second parameter of put call shoud be an object implementing Painter interface.

The correct sequence of code (with Nimbus LAF selected) is

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("TextPane[Enabled].backgroundPainter", 
    new javax.swing.plaf.nimbus.AbstractRegionPainter() {

        @Override
        protected AbstractRegionPainter.PaintContext getPaintContext() {
            return new AbstractRegionPainter.PaintContext(null, null, false);
        }

        @Override
        protected void doPaint(Graphics2D g, JComponent c, 
                int width, int height, Object[] extendedCacheKeys) {
            g.setColor(bgColor);
            g.fillRect(0, 0, width, height);
        }
    });
jtxtPane.putClientProperty("Nimbus.Overrides", defaults);
jtxtPane.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top