Sunflame
Sunflame

Reputation: 3186

JavaFx: auto resize TitledPane

I have a view with two TitledPanes in a SplitPane oriented vertically. I want when I collapse one of them, the other one to be resized to the Scene's height. Here is the code of my .fxml file:

<?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: Initial State When the first is collapsed: First 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?

Upvotes: 1

Views: 4512

Answers (4)

maroc
maroc

Reputation: 466

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.

Upvotes: 0

ruu
ruu

Reputation: 53

I had a similar problem and solved it by setting maxHeight to MAX_VALUE for both TitledPanes. No listeners and no wrappers (vbox, ...) are necessary in my case. I'm using openjfx 11.0.1.

Upvotes: 2

Tadasbub
Tadasbub

Reputation: 196

OK, here is a plan B:

Have fxml with a SplitPane

<?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

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);
   }
}

Upvotes: 0

Tadasbub
Tadasbub

Reputation: 196

<?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>

Upvotes: 3

Related Questions