0

我有这个上下文菜单,可以选择在新窗口中显示选项卡内容并关闭 TabPane 选项卡:

MenuItem item5 = new MenuItem("Open Tab In Stand-Alone Window");
        item5.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                System.out.println("Open Tab In Stand-Alone Window");
                Node test = tab.getContent();

                NavMenu.standAloneTab(test);
                tabPane.getTabs().remove(tab);

            }
        });

带有标签内容的新对话框:

public static void standAloneTab(Node tab)
    {
        final int xSize = 640;
        final int ySize = 480;
        final Color backgroundColor = Color.WHITESMOKE;

        Stage newConnDialog = new Stage();
        newConnDialog.initModality(Modality.WINDOW_MODAL);

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 20, 20, 20));

        grid.add(tab, 0, 0);

        Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
        newConnDialog.setScene(aboutDialogScene);
        newConnDialog.show();
    }

当我选择Open Tab In Stand-Alone Window带有选项卡内容的新窗口时,旧选项卡未关闭。如果我打电话tabPane.getTabs().remove(tab);关闭标签,新窗口是空的。

如何在不删除内容的情况下关闭选项卡?

编辑

我以这种方式更新了代码:

MenuItem item5 = new MenuItem("Open Tab in Stand-alone Window");
    item5.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override
        public void handle(ActionEvent e)
        {
            System.out.println("Open Tab In Stand-Alone Window");
            Node openedTab = tab.getContent();

            NavMenu.standAloneTab(tabPane, openedTab);
            //tabPane.getTabs().remove(tab);

        }
    });

和新的对话框:

public static void standAloneTab(final TabPane tabPane, final Node tab)
    {

        Stage newConnDialog = new Stage();
        newConnDialog.initModality(Modality.WINDOW_MODAL);

        newConnDialog.setOnShown(new EventHandler<WindowEvent>()
        {
            @Override
            public void handle(WindowEvent t)
            {
                tabPane.getTabs().remove(tab);
            }
        });

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 20, 20, 20));

        grid.add(tab, 0, 0);

        Scene aboutDialogScene = new Scene(grid, 700, 500, Color.WHITESMOKE);
        newConnDialog.setScene(aboutDialogScene);
        newConnDialog.show();
    }

我更新了代码,但得到了相同的结果。

4

2 回答 2

2

尝试调用tabPane.getTabs().remove(tab);

newConnDialog.setOnShown(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent t) {
        tabPane.getTabs().remove(tab);
    }
});

我希望这有帮助。

于 2013-07-03T09:33:57.123 回答
1

如何使用上下文菜单关闭选定选项卡以外的其他选项卡?

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tabs");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);

        TabPane tabPane = new TabPane();

        BorderPane borderPane = new BorderPane();
        for (int i = 0; i < 5; i++) {
            Tab tab = new Tab();
            tab.setText("Tab" + i);
            HBox hbox = new HBox();
            hbox.getChildren().add(new Label("Tab" + i));
            hbox.setAlignment(Pos.CENTER);
            tab.setContent(hbox);
            tabPane.getTabs().add(tab);

            ContextMenu contextMenu = new ContextMenu();
            MenuItem close = new MenuItem();
            MenuItem closeOthers = new MenuItem();
            MenuItem closeAll = new MenuItem();

            close.setText("Close");
            closeOthers.setText("Close Others");
            closeAll.setText("Close All");
            contextMenu.getItems().addAll(close,closeOthers,closeAll);
            tab.setContextMenu(contextMenu);

            ObservableList<Tab> tablist = tabPane.getTabs();


            close.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event)
                {
                    tabPane.getTabs().remove(tabPane.getSelectionModel().getSelectedItem());
                }
            });

            closeOthers.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event)
                {
                    tabPane.getTabs().removeAll();
                }
            });

            closeAll.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event)
                {
                    tabPane.getTabs().removeAll(tablist);
                }
            });
        }

        // bind to take available space
        borderPane.prefHeightProperty().bind(scene.heightProperty());
        borderPane.prefWidthProperty().bind(scene.widthProperty());

        borderPane.setCenter(tabPane);
        root.getChildren().add(borderPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
于 2015-11-20T09:29:47.013 回答