I have a JavaFX 2.2 project that uses Tabpane to dynamically open/close Tabs. I want to close a Tab when I click on save/close button on it.
Is it possible?
I though I could get the answer easily, but gotta say this is fxml project Javafx 2.2, there is 3 classes involved, main class, mainclassController and tabcontroller like so:
"main" = principal.java
public Tab abaUsuario(String nome) {
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Principal.class.getResource("controls/novoUsuarioForm.fxml"));
AnchorPane novoUsuario = (AnchorPane) loader.load();
//UsuarioDAO usrDAO = new UsuarioDAO();
//Usuario usr = new Usuario();
NovoUsuarioFormController nvu = new NovoUsuarioFormController();
nvu.setMainApp(this);
Tab t = new Tab(nome);
t.setContent(novoUsuario);
return t;
}catch (IOException ex ) {
Dialogs.showErrorDialog(primaryStage, ex.getMessage() , "Erro ao inserir Usuário", "JANELA DE ERRO");
//ex.getCause().printStackTrace();
}
return null;}
public void closeTab(){
baseWindowcontroller.closeUsuarioTab();
}
"mainController" = baseWindowController.java
@FXML
private void handleNovoUsuário(){
novoUsuarioTab = prime.abaUsuario("Novo usuario");
novoUsuarioTab.setClosable(true);
// int numtab = tab_base.getTabs().size();
// System.out.println(numtab);
tab_base.getTabs().add(novoUsuarioTab);
tab_base.getSelectionModel().selectLast();
//numtab = tab_base.getTabs().size();
}
public void closeUsuarioTab(){
// if (tab_base.getSelectionModel().isEmpty()){
// tab_base.getTabs().removeAll(novoUsuarioTab);
// tab_base.getTabs().remove(1);
//tab_base.getTabs().remove(novoUsuarioTab);
// }
Platform.runLater(new Runnable() {
@Override public void run() {
tab_base.getTabs().remove( tab_base.getSelectionModel().getSelectedIndex());
}
});
}
And
"tabController"= NewUserFormController.java
@FXML private void handlebtCancelar(){
prime.closeTab();}
prime = Principal
I have set Principal.java the mainApp for the controllers
As you can see I have tried a lot of possibilities.
You can assume that the button will be clicked on the currently active tab, so close this active tab. Do on button action:
Edit:
It is not working because you are not using the Controller that was created by FXMLLoader, your abaUsuario() method should be
Also you don't need to do
Platform.runLater()
when closing the tab.