Set inactive background color to JTextField for each component under Windows LAF
I want to set an inactive background color for the JTextField for each component. (Inactive colors are displayed on call setEditable(false)
).
The call UIManager.put("TextField.inactiveBackground", new ColorUIResource(Color.YELLOW));
sets the inactive color altogether.
This can be done in Nimbus LAF as described here: http://docs.oracle.com/javase/7/docs/api/javax/swing/plaf/nimbus/package-summary.html . Can this be done using Windows LAF?
+3
source to share
1 answer
I found a solution. Not a very pretty solution, but a solution nonetheless:
Extend the JTextField class and override the paintComponent method to draw a rectangle of the desired color.
class CustomTextField extends JTextField {
private Color inactiveColor = UIManager.getColor("TextField.inactiveBackground");
public void setDisabledBackgroundColor(Color inactiveColor) {
this.inactiveColor = inactiveColor;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
if (!isEditable() || !isEnabled()) {
setOpaque(false);
g.setColor(inactiveColor);
g.fillRect(0, 0, getWidth(), getHeight());
} else {
setOpaque(true);
}
super.paintComponent(g);
}
}
0
source to share