我有一個選項,以顯示選項卡的內容到新的窗口和關閉TabPane標籤這個上下文菜單如何關閉標籤:時顯示的內容在新窗口中的JavaFX
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();
}
我更新的代碼,但我得到了相同的結果。
您將標籤(openedTab變量是選項卡的內容)的內容傳遞給standAloneTab方法。並且您正試圖從標籤中刪除內容。但是,您應該從tabPane中刪除選項卡本身,而不是它的內容。試一試... – Ramazan
'您將標籤的內容(opensTab變量是選項卡的內容)傳遞給standAloneTab方法。並且您正試圖從選項卡中刪除內容。「是的 – user1285928
但是,您必須爲tabPane.getTabs().get()方法提供一個*** Tab對象,而不是其他一些** * Node ***對象,以便能夠刪除選項卡。這肯定是錯誤的,你不能以這種方式刪除標籤。 – Ramazan