If you're looking to dynamically update a CSS class of a component in Java code, one way to do so is by using an AttributeAppender:
component.add(new AttributeAppender("class", true, new Model<String>("foo"), " "));
You can also utilize a utility method or class like this:
component.add(WicketUtils.cssClassAppender("foo"));
However, removing a CSS class may not be as straightforward.
To remove all existing CSS classes, you can clear the class attribute entirely:
component.add(new SimpleAttributeModifier("class", ""));
But if you want to keep other CSS classes intact, this approach may not be suitable.
This solution is based on Wicket 1.4, but advice for later versions is welcome too.