java - Is it possible to change the component style at runtime? -
i use swing + nimbus style component. want change style of component @ runtime "nimbus.overrides".
private void setexceptionstate() { //password.setbackground(new color(200,0,0,120)); uidefaults overrides = new uidefaults(); overrides.put("passwordfield.background", color.red); password.putclientproperty("nimbus.overrides", overrides); password.revalidate(); password.updateui(); } private void resetexceptionstate() { //password.setbackground(color.white); uidefaults overrides = new uidefaults(); overrides.put("passwordfield.background", color.white); password.putclientproperty("nimbus.overrides", overrides); }
the first time set overrides, lets setexceptionstate() method works. red background. second time use nothing happens. seems, overrides evaluated once.
what want introduce new state ofthe passwordfield , style different. there possibility so?
best regards,
yggdrasil
yes, possible , nimbus listens changes of "nimbus.overrides" - just: not uninstall properties if !instanceof uiresource
that's case background, foreground, font @ least (might others well)
in context, did install not-uiresource of red initially, telling laf not touch again - , complies :-)
the way make work, null background before setting new overrides, in:
private void setexceptionstate(jcomponent password) { password.setbackground(null); uidefaults overrides = new uidefaults(); overrides.put("passwordfield.background", color.red); password.putclientproperty("nimbus.overrides", overrides); } private void resetexceptionstate(jcomponent password) { password.setbackground(null); uidefaults overrides = new uidefaults(); overrides.put("passwordfield.background", color.white); password.putclientproperty("nimbus.overrides", overrides); }
update
actually, above doesn't answer real question:
introduce new state of passwordfield , style different
nimbus indeed allow add custom states (though outcome unpredicable, unloved youngest child of synth ;-) way go is
- implement custom state
- register additonal state ui-defaults
- attach properties state needed
all configuration has done after laf installed , before first jpasswordfield instantiated, (didn't test) posing problems if laf switched @ runtime.
protected void installcustompasswordfieldstate() { // implement custom state state<jpasswordfield> state = new state<jpasswordfield>("invalid") { @override protected boolean isinstate(jpasswordfield c) { object invalid = c.getclientproperty("invalid"); return boolean.true.equals(invalid); } }; uidefaults defaults = uimanager.getlookandfeeldefaults(); // register available states // note: couldn't find way grab available states // guesswork defaults.put("passwordfield.states", "enabled, focused, invalid"); // install custom state defaults.put("passwordfield.invalid", state); // install properties custom state // note: background has no effect defaults.put("passwordfield[invalid].background", color.red); javax.swing.painter<jcomponent> p = new javax.swing.painter<jcomponent>() { @override public void paint(graphics2d g, jcomponent object, int width, int height) { g.setcolor(color.red); // crude - overpainting complete area, better! g.fillrect(0, 0, width, height); } }; // using painter has effect defaults.put("passwordfield[invalid].backgroundpainter", p); } // example usage, toggling // new property (for simplicity implemented clientproperty // toggle invalid state action reset = new abstractaction("reset") { @override public void actionperformed(actionevent e) { boolean isinvalid = boolean.true.equals(field.getclientproperty("invalid")); if (isinvalid) { field.putclientproperty("invalid", null); } else { field.putclientproperty("invalid", boolean.true); } field.repaint(); } };
Comments
Post a Comment