我有这个上下文菜单,可以选择将标签内容显示到新窗口并关闭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();
}
我更新了代码但得到了相同的结果。
答案 0 :(得分:2)
尝试将tabPane.getTabs().remove(tab);
称为
newConnDialog.setOnShown(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
tabPane.getTabs().remove(tab);
}
});
我希望这会有所帮助。
答案 1 :(得分: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();
}
}