Try this:
add(new Label("msg", messageModel)) {
@Override public boolean isVisible() {
return !messageModel.getObject().equals(message);
}
};
The text field needs to know when it should be rendered. Note that you need to set setOutputMarkupId(true)
and setOutputMarkupPlaceholderTag(true)
on the Label
if you want to make the component visible via Ajax, i.e. the submit
component is for example an AjaxSubmitButton
.
When performing a more complex task than a String
comparison, you should rather call setVisible
from an overriden onConfigure
method, as mentioned in the comments. isVisible
might be called plenty of times during the rendering phase such that you want to avoid computations. This would look like the following:
add(new Label("msg", messageModel)) {
@Override protected void onConfigure() {
super.onConfigure();
setVisible(!messageModel.getObject().equals(message));
}
};
Never forget to call the super
method, though.
In order to improve code readability, I personally prefer to override the isVisible
method for easy tasks like the String
comparison you require. The JIT compiler usually takes care of the rest.