How can I close a Javafx 2.2 Tab when clicking a save/cancel button inside it?

1.4k views Asked by At

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.

2

There are 2 answers

2
Uluk Biy On BEST ANSWER

You can assume that the button will be clicked on the currently active tab, so close this active tab. Do on button action:

tabPane.getTabs().remove( tabPane.getSelectionModel().getSelectedIndex() );

Edit:
It is not working because you are not using the Controller that was created by FXMLLoader, your abaUsuario() method should be

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();

        NovoUsuarioFormController nvu = (NovoUsuarioFormController) loader.getController();
        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;
}

Also you don't need to do Platform.runLater() when closing the tab.

0
ItachiUchiha On

You can remove the current tab from the ObservableList<Tab> in the TabPane, on the action of the Save/Cancel button.

MCVE :

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
}

    @Override
    public void start(Stage primaryStage) throws Exception {
        TabPane tabPane = new TabPane();

        for(int i=1;i<=5;i++) {
            Tab tab = new Tab("Tab " + i);
            Button button = new Button("Close Current Tab");
            button.setOnAction(e -> tabPane.getTabs().remove(tab));
            tab.setContent(new StackPane(button));
            tabPane.getTabs().add(tab);
        }

        VBox container  = new VBox(tabPane);
        tabPane.prefHeightProperty().bind(container.heightProperty());
        Scene scene = new Scene(container, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}