It's pretty straightforward to dynamically add a CSS class to a component in Java code using an AttributeAppender:
component.add(new AttributeAppender("class", true, new Model<String>("foo"), " "));
Or, if you've extracted the above into an appropriate utility method or class, simply something like:
component.add(WicketUtils.cssClassAppender("foo"));
But how can I remove a CSS class?
You can easily remove all CSS classes by clearing the class attribute altogether:
component.add(new SimpleAttributeModifier("class", ""));
...but that is not acceptable if the component has other CSS classes that you do not wish to remove.
Wicket 1.4 (but feel free to post advice specific to later versions too).