2016-06-27 60 views
0

我試圖獲得手風琴的UI行爲,其中用戶可以彈出任何TitledPane到窗口,並將窗口彈回到手風琴內的TitledPane將標題窗口內容彈出窗口

但是,當彈出摺疊的TitledPane時,內容未在Stage中正確對齊,並且如果no窗格展開,它甚至不會顯示。

附加是顯示問題的最小示例 - 請注意,我保留了兩個佔位符窗格,以避免內容節點(我的情況下爲A VBox)多次出現在場景圖中。我曾嘗試在VBox上設置preferredSizevisible屬性,並在顯示前後調用layout,甚至以編程方式展開標題窗格,但似乎沒有任何效果。結果

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     TitledPane t1 = new TitledPane(); 
     TitledPane t2 = new TitledPane(); 
     Accordion accordion = new Accordion(t1, t2); 
     t1.setContent(buildComponent("Pane 1", t1, accordion)); 
     t2.setContent(buildComponent("Pane 2", t2, accordion)); 

     primaryStage.setScene(new Scene(accordion, 300, 300)); 
     primaryStage.show(); 
    } 

    private VBox buildComponent(String name, TitledPane titledPane, Accordion holder) { 
     final Button popout = new Button("Pop out"); 
     titledPane.setGraphic(popout); 
     titledPane.setText(name); 
     final VBox component = new VBox(new Label(name), new TableView<>()); 
     final Pane placeholder1 = new Pane(); 
     final Pane placeholder2 = new Pane(); 
     Stage st = new Stage(); 
     st.setScene(new Scene(placeholder1, 300, 300)); 

     popout.setOnAction(event -> { 
      if (!st.equals(component.getScene().getWindow())) { 
       holder.getPanes().remove(titledPane); 
       titledPane.setContent(placeholder2); 
       st.getScene().setRoot(component); 
       st.show(); 
      } 
     }); 

     st.setOnHidden(windowEvent -> { 
      st.getScene().setRoot(placeholder1); 
      titledPane.setContent(component); 
      holder.getPanes().add(titledPane); 
     }); 

     return component; 
    } 

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

插圖:
時候,沒有窗格中展開:當其他窗格中展開
Result when no pane is expanded
結果。注意標籤是多麼的不可見:當窗格中展開
Result when other pane is expanded. Note how the label is not visible.
結果 - 這是我想在所有情況下,結果:
Result when pane is expanded - this is the result I want to have in all cases.

回答

0

經過一些玩弄我已經找到一種方法來避開這個問題,通過使用AnchorPane!而非直接設置Stage的根和TitledPane的內容:

private VBox buildComponent(String name, TitledPane titledPane, Accordion holder) { 
    final Button popout = new Button("Pop out"); 
    titledPane.setGraphic(popout); 
    titledPane.setText(name); 
    final VBox component = new VBox(new Label(name), new TableView<>()); 
    final AnchorPane placeholder1 = new AnchorPane(); 
    final AnchorPane placeholder2 = new AnchorPane(); 
    AnchorPane.setTopAnchor(component, 0D); 
    AnchorPane.setBottomAnchor(component, 0D); 
    AnchorPane.setLeftAnchor(component, 0D); 
    AnchorPane.setRightAnchor(component, 0D); 
    Stage st = new Stage(); 
    st.setScene(new Scene(placeholder1, 300, 300)); 
    titledPane.setContent(placeholder2); 
    placeholder2.getChildren().add(component); 

    popout.setOnAction(event -> { 
     if (!st.equals(component.getScene().getWindow())) { 
      holder.getPanes().remove(titledPane); 
      placeholder2.getChildren().clear(); 
      placeholder1.getChildren().add(component); 
      st.show(); 
     } 
    }); 

    st.setOnHidden(windowEvent -> { 
     placeholder1.getChildren().clear(); 
     placeholder2.getChildren().add(component); 
     holder.getPanes().add(titledPane); 
    }); 

    return component; 
} 

這種新buildComponent還添加了VBoxTitledPane,所以在這個問題中調用TitledPane#setContent應該被刪除。

我仍然很想知道爲什麼會出現此問題,或者如果有方法可以解決問題,而無需使用另一個窗格包裝我的窗格(在我的情況下爲AnchorPane)。