2017-01-23 29 views
2

我打破了我的頭,使這項工作。基本上我有2個選項卡。最初,其中一個有100px大的內容(我不知道大小,這只是一個例子),另一個沒有內容。 經過一番事件之後,我在之前的空白標籤中加入了一個200像素的高度。TabPane縮小到最大的孩子

聽到一切都好。 TabPane調整大小後(正確調用requestLayout())並適合新內容。

現在,如果我刪除200像素的內容,我希望選項卡窗格返回到100像素高,即仍然留在標籤上的最大項目,但仍保留在200px,留下很多空白空間。有人能提供我如何解決這個問題的線索嗎?

這裏是我的演示代碼:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Tab; 
import javafx.scene.control.TabPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class TabPaneTest extends Application { 


    @Override 
    public void start(Stage primaryStage) throws Exception { 
     TabPane tabPane = new TabPane(); 
     Tab tab1 = new Tab("Test 1"); 
     Tab tab2 = new Tab("Test 2"); 

     final Pane tab2Content = new Pane(); 
     tab2Content.getStyleClass().add("tab2Content"); 
     tab2.setContent(tab2Content); 

     tabPane.getTabs().addAll(tab1, tab2); 

     Button addSquare = new Button("Add"); 
     addSquare.setOnAction(event -> { 
      final Pane tab1Content = new Pane(); 
      tab1Content.getStyleClass().add("tab1Content"); 

      tab1.setContent(tab1Content); 

      tabPane.requestLayout(); 
     }); 

     Button removeSquare = new Button("Remove"); 
     removeSquare.setOnAction(event -> { 
      tab1.setContent(null); 
      tabPane.requestLayout(); 
     }); 
     HBox buttons = new HBox(addSquare, removeSquare); 


     VBox vBox = new VBox(tabPane, buttons); 
     Scene scene = new Scene(vBox, 500, 400); 
     scene.getStylesheets().add(LargeTooltipSample.class.getResource("tabPaneTest.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
} 

,這裏是我的CSS(只是爲了更好地可視化內容):

.tab2Content{ 
    -fx-background-color: red; 
    -fx-max-height: 100; 
    -fx-pref-height: 100; 
    -fx-max-width: 100; 
    -fx-pref-width: 100; 
} 

.tab1Content{ 
    -fx-background-color: green; 
    -fx-max-height: 200; 
    -fx-pref-height: 200; 
    -fx-max-width: 200; 
    -fx-pref-width: 200; 
} 

在此先感謝

回答

0

試試這個:

@Override 
public void start(Stage primaryStage) throws Exception { 
     TabPane tabPane = new TabPane(); 
     Tab tab1 = new Tab("Test 1"); 
     Tab tab2 = new Tab("Test 2"); 

     final Pane tab2Content = new Pane(); 
     final Pane tab1Content = new Pane(); 

     tab2Content.getStyleClass().add("tab2Content"); 
     tab2.setContent(tab2Content); 

     tabPane.getTabs().addAll(tab1, tab2); 

     Button addSquare = new Button("Add"); 
     Button removeSquare = new Button("Remove"); 
     HBox buttons = new HBox(addSquare, removeSquare); 
     VBox vBox = new VBox(tabPane, buttons); 


     double[] tabPaneOriginalHeight = new double[1];//Used to hold tabPane's original height. 

     addSquare.setOnAction(event -> { 

     System.out.println(tabPane.getHeight()); 
     tab1Content.getStyleClass().add("tab1Content"); 
     tab1.setContent(tab1Content);  
     System.out.println(tabPane.getHeight()); 
     tabPane.setPrefHeight(200);//sets the height to 200 after adding tabl1 content 
     tabPane.requestLayout(); 


    }); 


    removeSquare.setOnAction(event -> { 
     System.out.println(tabPane.getHeight()); 

     tab1Content.getStyleClass().remove("tab1Content"); 
     tab1.setContent(tab1Content); 
     System.out.println(tabPane.getHeight()); 
     tabPane.setPrefHeight(100);//sets the height to 100 after removing tab1 content 
     tabPane.requestLayout(); 
    }); 




     Scene scene = new Scene(vBox, 500, 400); 
     scene.getStylesheets().add(JavaFXApplication16.class.getResource("tabPaneTest.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
+0

有人會有更好的方法通過綁定。 – Sedrick

+0

這個例子適用於這個例子,但想象下面的場景: 200px部分已經到位了。然後你將它刪除,只留下100像素。它不會通過使用您的方法重新縮放到100像素,因爲「原始高度」是200像素,而不是100像素:( – fditz

+0

嗯,而不是tabPane.setPrefHeight(tabPaneOriginalHeight [0]);試試tabPane.setPrefHeight(100); – Sedrick