2013-07-03 46 views
0

我有一個選項,以顯示選項卡的內容到新的窗口和關閉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(); 
    } 

我更新的代碼,但我得到了相同的結果。

+0

您將標籤(openedTab變量是選項卡的內容)的內容傳遞給standAloneTab方法。並且您正試圖從標籤中刪除內容。但是,您應該從tabPane中刪除選項卡本身,而不是它的內容。試一試... – Ramazan

+0

'您將標籤的內容(opensTab變量是選項卡的內容)傳遞給standAloneTab方法。並且您正試圖從選項卡中刪除內容。「是的 – user1285928

+0

但是,您必須爲tabPane.getTabs().get()方法提供一個*** Tab對象,而不是其他一些** * Node ***對象,以便能夠刪除選項卡。這肯定是錯誤的,你不能以這種方式刪除標籤。 – Ramazan

回答

2

嘗試調用tabPane.getTabs().remove(tab);作爲

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

我希望這有助於。

+0

我更新了代碼,但得到了相同的結果。 – user1285928

+0

您是否看到我之前的評論?小心節點類的類型... – Ramazan

+0

好的,謝謝你的答案! – user1285928

1

如何使用contex菜單關閉除選定選項卡以外的其他選項卡?

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

} 
相關問題