At the moment i am working with JavaFX building a little Application that has a lot of configurable parameters. For the reason of not getting lost on a dialog with thousand of different checkboxes, textfields and so on, the parameter controls should be grouped with the JavaFX control TabPane. A TabPane was quickly done but soon i realized that it is not so easy to prevent the user from changing the current tab if the parameters grouped in this tab have invalid values. Instead of switching to the selected tab i rather like to show a validation error message. So i did some reading on the JavaFX API but there is no way out of the box to achieve this behavior. You can do some work if the tab has changed but not before it is changed. If you have a similar issue with the TabPane maybe my solution will work for you :-)
class StateAwareChangeListener implements ChangeListener<Tab>
{
/**
* go to previous tab if validation fails and this flag is true.
*/
private boolean roleback;
/**
* If validation is ok change the tab.
*
* {@inheritDoc}
*
*/
@Override
public void changed(ObservableValue<? extends Tab> arg0, Tab arg1, Tab arg2)
{
if (roleback) {
roleback = false;
return;
}
boolean validateConfig = validateIfChangeIsPossible();
if (validateConfig == false) {
roleback = true;
tabPane.getSelectionModel().select(arg1);
}
}
}
This listener calls the method validateIfChangeIsPossible that you have to implement by yourself. If changing the tab is possible return true. If false the old tab is selected again. You can also parse the new tab as a parameter into validateIfChangeIsPossible, if you need the information.
Finally add this class as a ChangeListener to the selectedItemProperty of the TabPane:
tabPane.getSelectionModel().selectedItemProperty().addListener(new StateAwareChangeListener());
No comments:
Post a Comment