2016-05-17 123 views
0

我想使用(JavaFX)TabPane顯示20個不同選項卡的內容。這與標準TabPane一起工作良好,但是,當窗格觸及一定數量的選項卡時,可以單擊按鈕/組合框以單擊其中一個未顯示的選項卡。帶有多行選項卡的Javafx TabPane

我正在設計一個功能,將用於觸摸屏,所以這是不理想的。我認爲有兩個單獨的選項卡會更直觀。

如何將兩行選項卡添加到TabPane中,或者,可以做些什麼來實現類似的效果?先謝謝了。

下面是一些示例代碼重現我的意思:

public class TabTest extends Application { 


public static void main(String args[]) { 
    Application.launch(args); 
} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    primaryStage.setTitle("Tabs Test"); 
    Group root = new Group(); 
    Scene scene = new Scene(root, 450, 250, Color.WHITE); 

    TabPane tabPane = new TabPane(); 
    BorderPane borderPane = new BorderPane(); 

    for(int i = 0; i < 20; i++) 
    { 
     Tab tab = new Tab(); 
     tab.setText("Tab " + i); 
     HBox hbox = new HBox(); 
     hbox.getChildren().add(new Label("Tab " + i)); 
     tab.setContent(hbox); 
     tabPane.getTabs().add(tab); 
    } 

    borderPane.prefHeightProperty().bind(scene.heightProperty()); 
    borderPane.prefWidthProperty().bind(scene.widthProperty()); 

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

Link to tabbed view, since I can't post images yet

+0

您應該發佈您嘗試過的代碼,但目前無法運行。 – ManoDestra

+1

@ManoDestra謝謝,我用示例代碼和圖像鏈接更新了我的問題。 – Kornephoros

回答

0

TabPane的標籤頭區域基本上是一個StackPane,因此,我認爲它不是那麼容易創造了兩排標籤而不是一個。

我的想法是隱藏TabPane的原始選項卡,並將一組ToggleButton對象放在ToggleGroup中,然後將選擇的切換與選項卡綁定。

這樣你可以將「標籤」添加到你想要的任何容器(流,網格等)。

真的最少的樣品:

Main.java

public class Main extends Application { 

    TabPane tabPane; 
    private ToggleGroup toggleGroup; 

    @Override 
    public void start(Stage primaryStage) { 

     Group root = new Group(); 
     Scene scene = new Scene(root, 700, 400, Color.WHITE); 

     primaryStage.setTitle("Tabs Test"); 

     toggleGroup = new ToggleGroup(); 
     toggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() { 

      @Override 
      public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) { 
       if (newValue == null) 
        toggleGroup.selectToggle(oldValue); 
       else 
        tabPane.getSelectionModel().select((Tab) newValue.getUserData()); 
      } 
     }); 

     tabPane = new TabPane(); 
     tabPane.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 

     VBox vboxToggleOuterContainer = new VBox(); 
     HBox hboxToggleFirstRow = new HBox(); 
     HBox hboxToggleSecondRow = new HBox(); 

     vboxToggleOuterContainer.getChildren().addAll(hboxToggleFirstRow, hboxToggleSecondRow); 

     for (int i = 0; i < 20; i++) { 
      Tab tab = new Tab(); 
      tab.setText("Tab " + i); 
      HBox hbox = new HBox(); 
      hbox.getChildren().add(new Label("Tab " + i)); 
      tab.setContent(hbox); 
      tabPane.getTabs().add(tab); 

      ToggleButton tb = new ToggleButton("Tab" + i); 
      tb.setToggleGroup(toggleGroup); 
      tb.setUserData(tab); 

      if (i < 10) 
       hboxToggleFirstRow.getChildren().add(tb); 
      else 
       hboxToggleSecondRow.getChildren().add(tb); 
     } 

     toggleGroup.selectToggle(toggleGroup.getToggles().get(0)); 

     VBox vbox = new VBox(); 
     vbox.getChildren().addAll(vboxToggleOuterContainer, tabPane); 
     vbox.fillWidthProperty().set(true); 
     root.getChildren().add(vbox); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

application.css

.tab-pane { 
    -fx-skin: "com.sun.javafx.scene.control.skin.TabPaneSkin"; 
    -fx-tab-min-height: 0; 
    -fx-tab-max-height: 0; 
} 

.tab-pane .tab-header-area { 
    -fx-padding: 0 0 0 0; 
} 

.tab-pane .tab-header-area .headers-region .tab { 

    -fx-padding: 0 0 1 0; 
} 

的例子實在是微乎其微,只是顯示的辦法,你可以提高CSS有更多的控制權e花哨的(或者如果你想的話,我也可以更新它,如果我有時間的話)。

+0

感謝您的回答。我希望有一個更乾淨的方法來做到這一點,但這個例子正是我想要的。 – Kornephoros

相關問題