简体   繁体   English

JavaFx:自动调整TitledPane的大小

[英]JavaFx: auto resize TitledPane

I have a view with two TitledPane s in a SplitPane oriented vertically. 我在垂直定向的SplitPane有两个TitledPane的视图。 I want when I collapse one of them, the other one to be resized to the Scene 's height. 我希望当我折叠其中一个时,将另一个调整为Scene的高度。 Here is the code of my .fxml file: 这是我的.fxml文件的代码:

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<BorderPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:id="pane"
            fx:controller="stackoverflow.three.Controller">
    <center>
        <SplitPane fx:id="split" orientation="VERTICAL">
            <TitledPane fx:id="first" text="First">
                <TableView>
                    <columns>
                        <TableColumn text="Test"/>
                    </columns>
                </TableView>
            </TitledPane>
            <TitledPane fx:id="second" text="Second">
                <TableView>
                    <columns>
                        <TableColumn text="Test"/>
                    </columns>
                </TableView>
            </TitledPane>
        </SplitPane>
    </center>
</BorderPane>

Here are some sreenshots: The initial state: 以下是一些快照:初始状态: 初始状态 When the first is collapsed: 当第一个折叠时: 第一次倒塌

As you can see there is a gap at the bottom of the view, if I collapse the first one, but I don't want that gap. 如您所见,如果我折叠第一个视图,则视图的底部有一个缝隙,但我不希望有此缝隙。

I've tried to se the maxHeight for example to Infinity, but then the auto move up to the firs one is not working... Any ide what can I do? 我试图将maxHeight例如设置为Infinity,但随后自动向上移动到第一个却不起作用...任何想法我该怎么办?

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>

<BorderPane fx:id="pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow.three.Controller">
    <center>
      <VBox prefHeight="800.0">
         <children>
               <TitledPane fx:id="first" text="First">
                  <content>
                      <TableView prefHeight="2000.0">
                          <columns>
                              <TableColumn text="Test" />
                          </columns>
                      </TableView>
                  </content>
               </TitledPane>
               <TitledPane fx:id="second" text="Second">
                  <content>
                      <TableView prefHeight="2000.0">
                          <columns>
                              <TableColumn text="Test" />
                          </columns>
                      </TableView>
                  </content>
               </TitledPane>
         </children>
      </VBox>
    </center>
</BorderPane>

I had a similar problem and solved it by setting maxHeight to MAX_VALUE for both TitledPanes. 我遇到了类似的问题,并通过将两个TitledPanes的maxHeight设置为MAX_VALUE来解决了该问题。 No listeners and no wrappers (vbox, ...) are necessary in my case. 在我的情况下,不需要侦听器和包装器(vbox,...)。 I'm using openjfx 11.0.1. 我正在使用openjfx 11.0.1。

OK, here is a plan B: 好,这是计划B:

Have fxml with a SplitPane 使用带有SplitPane的fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>

<BorderPane fx:id="pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stackoverflow.three.Controller">
    <center>
      <SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="800.0">
         <items>
               <TitledPane fx:id="first" text="First">
                  <content>
                      <TableView prefHeight="2000.0">
                          <columns>
                              <TableColumn text="Test" />
                          </columns>
                      </TableView>
                  </content>
               </TitledPane>
               <TitledPane fx:id="second" text="Second">
                  <content>
                      <TableView prefHeight="2000.0">
                          <columns>
                              <TableColumn text="Test" />
                          </columns>
                      </TableView>
                  </content>
               </TitledPane>
         </items>
      </SplitPane>
    </center>
</BorderPane>

to both TitledPane objects add listener to their boolean expanded property 向两个TitledPane对象添加侦听器到它们的boolean expanded属性

expanded.addListener(new ChangeListener<Boolean>() {
     @Override
     public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
         resizeSplitPane();
     }
 });

private void resizeSplitPane(){
   // reposition split pane divider here depending on the status of both 
   // TableViews
   if(!tableView1.isExpanded() && tableView2.isExpanded()){
      splitPane.setDividerPosition(0, 0.1);
   }else if(tableView1.isExpanded() && !tableView2.isExpanded()){
      splitPane.setDividerPosition(0, 0.9);
   }else{
      splitPane.setDividerPosition(0, 0.5);
   }
}

I managed to figure something out based on the answer here: Animate splitpane divider 我根据这里的答案设法弄清楚了一些东西: 动画分割窗格分隔线

    titledPane.expandedProperty().addListener((observable, oldValue, newValue) ->
    {
        double target = d1Orig;
        if( !newValue )
        {
            d1Orig = splitPane.getDividers().get(0);
            target = 0.0;
        }
        KeyValue keyValue = new KeyValue(splitPane.getDividers().get(0).positionProperty(), target);
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), keyValue));
        timeline.play();
    });

d1Orig is a double to save the original split pane position before collapsing. d1Orig是一个double值,用于在折叠前保存原始的拆分窗格位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM