问题
I have a JTextPane
that haspane.setEditable(false)
which forces it to have a 'greyed-out' background color.
After trying setBackground(Color.WHITE)
(which doesn't work), I tried looking around the net for an answer but not managed to find one yet.
Can anyone help me on this one please?
回答1:
Note that for some Look and Feel like Nimbus that don't respect background property, you can use this code :
private static class Painter extends javax.swing.plaf.nimbus.AbstractRegionPainter {
private final Color color;
private Painter(Color color) {
this.color = color;
}
@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(c.isEnabled() ? c.getBackground() : color);
g.fillRect(0, 0, width, height);
}
}
This defines a new custom painter for the background. For Nimbus, apply it to your JTextPane jtp
this way:
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
Painter painter = new Painter(color);
String key = "TextPane[Disabled].backgroundPainter";
defaults.put(key, painter);
jtp.putClientProperty("Nimbus.Overrides", defaults);
jtp.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
回答2:
Actually try this one:
UIManager.put("TextPane.disabledBackground", Color.WHITE);
I think it should be TextPane.disabledBackground
, if not try : TextPane.inactiveBackground
To change text back ground color I believe: Try setDisabledTextColor on the pane.
回答3:
I found out what it was - it was because I had pane.setOpaque(false)
, I couldn't change the background color without first either removing this or changing it to true.
setBackground(Color.white)
worked after altering this.
来源:https://stackoverflow.com/questions/34681453/how-can-i-change-the-background-color-of-an-uneditable-jtextpane-in-java