2016-06-27 63 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)。