【问题标题】:How can I close a Javafx 2.2 Tab when clicking a save/cancel button inside it?单击其中的保存/取消按钮时,如何关闭 Javafx 2.2 选项卡?
【发布时间】:2015-06-24 04:20:26
【问题描述】:

我有一个使用 Tabpane 动态打开/关闭选项卡的 JavaFX 2.2 项目。当我点击它上的保存/关闭按钮时,我想关闭它。

有可能吗?

虽然我可以很容易地得到答案,但不得不说这是 fxml 项目 Javafx 2.2,涉及 3 个类,主类、mainclassController 和 tabcontroller,如下所示:

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

还有

"tabController"=NewUserFormController.java

 @FXML private void handlebtCancelar(){  
           prime.closeTab();} 

素数 = 校长

我已将 Principal.java 设置为控制器的 mainApp

如你所见,我尝试了很多可能性。

【问题讨论】:

    标签: java javafx fxmlloader


    【解决方案1】:

    您可以假设将在当前活动选项卡上单击该按钮,因此请关闭此活动选项卡。执行按钮操作:

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

    编辑:
    它不起作用,因为您没有使用 FXMLLoader 创建的控制器,您的 abaUsuario() 方法应该是

    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;
    }
    

    另外你在关闭标签时也不需要Platform.runLater()

    【讨论】:

    • @RonaldoLinspinho,查看更新。如果仍然无法正常工作,请更详细地描述此无法正常工作的问题。
    • 一个小错误可能会导致我们弄乱代码。谢谢!
    【解决方案2】:

    您可以通过 Save/Cancel 按钮的操作从 TabPane 中的 ObservableList<Tab> 中删除当前选项卡。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多