I have a method that sets the width and height of the treeview to its parent's width and height, for some reason this isn't working. I had it working with a listview, but I decided a treeview would work best in my case.

Ill supply the full FXML, and it's controller, as well as the start method.

Start

    private Stage primaryStage;
    private VBox rootLayout;

    @Override
    public void start(Stage primaryStage)
    {
        this.primaryStage = primaryStage;
        primaryStage.setTitle("PokeBase - Pokemon Database Application");

        //System.out.println(System.getProperty("java.class.path"));

        try
        {
            // Load the root layout from the fxml file
            FXMLLoader loader = new FXMLLoader(PokeBase.class.getResource("/com/geodox/pokebase/main/PokeBaseGUI.fxml"));
            loader.setController(new PokeBaseGUIController());
            final PokeBaseGUIController controller = (PokeBaseGUIController) loader.getController();
            rootLayout = (VBox) loader.load();
            Scene scene = new Scene(rootLayout);

            //Broken, will re-enable when fixed, change resizable to true
            /*scene.heightProperty().addListener(new ChangeListener<Number>() {
                @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneHeight, Number newSceneHeight) {
                    controller.setListViewSize();
                }
            });
            scene.widthProperty().addListener(new ChangeListener<Number>() {
                @Override public void changed(ObservableValue<? extends Number> observableValue, Number oldSceneWidth, Number newSceneWidth) {
                    controller.setListViewSize();
                }
            });*/

            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();

            controller.initSetFocus();
        }
        catch (IOException e)
        {
            // Exception gets thrown if the fxml file could not be loaded
            e.printStackTrace();
        }
    }

FXML

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<VBox maxHeight="720.0" maxWidth="1280.0" minHeight="720.0" minWidth="1280.0" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <children>
            <SplitPane dividerPositions="0.225" layoutX="155.0" layoutY="85.0" prefHeight="400.0" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
                     <children>
                        <SplitPane fx:id="hSplitPane" dividerPositions="0.05" layoutX="14.0" layoutY="135.0" orientation="VERTICAL" prefHeight="718.0" prefWidth="285.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <items>
                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" SplitPane.resizableWithParent="false">
                                 <children>
                                    <TextField fx:id="searchBar" layoutX="75.0" prefHeight="33.0" prefWidth="283.0" promptText="Search..." AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0">
                                       <font>
                                          <Font size="15.0" />
                                       </font>
                                    </TextField>
                                 </children>
                              </AnchorPane>
                              <ScrollPane fx:id="scrollPane" hbarPolicy="NEVER" prefHeight="677.0" prefWidth="283.0" vbarPolicy="ALWAYS" SplitPane.resizableWithParent="false">
                                 <content>
                                    <TreeView fx:id="treeView" />
                                 </content>
                              </ScrollPane>
                          </items>
                        </SplitPane>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TabPane fx:id="tabPane" layoutX="411.0" layoutY="244.0" prefHeight="718.0" prefWidth="987.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
      </children>
    </AnchorPane>
  </children>
</VBox>

Controller

package com.geodox.pokebase.main;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;

public class PokeBaseGUIController
{
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ScrollPane scrollPane;

    @FXML
    private SplitPane hSplitPane;

    @FXML
    private TreeView<String> treeView;

    @FXML
    private AnchorPane treeViewPane;

    @FXML
    private TextField searchBar;

    @FXML
    private TabPane tabPane;

    @FXML
    void initialize()
    {
        assert scrollPane != null : "fx:id=\"scrollPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert hSplitPane != null : "fx:id=\"hSplitPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert treeView != null : "fx:id=\"treeView\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert searchBar != null : "fx:id=\"searchBar\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert tabPane != null : "fx:id=\"tabPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";

        setTreeViewSize();
    }

    private void setTreeViewItems()
    {
        TreeItem<String> pokeBaseItem = new TreeItem<String> ("PokeBase");
        pokeBaseItem.setExpanded(true);

        TreeItem<String> pokemonItems = new TreeItem<String> ("Pokemon");
        TreeItem<String> abilitiesItem = new TreeItem<String> ("Abilities");
        TreeItem<String> movesItem = new TreeItem<String> ("Moves");
        TreeItem<String> typesItem = new TreeItem<String> ("Types");

        pokeBaseItem.getChildren().add(pokemonItems);
        pokeBaseItem.getChildren().add(abilitiesItem);
        pokeBaseItem.getChildren().add(movesItem);
        pokeBaseItem.getChildren().add(typesItem);

        treeView = new TreeView<String> (pokeBaseItem);
    }

    public String getSearchText()
    {
        return searchBar.getText();
    }

    public void setTreeViewSize()
    {
        scrollPane.setPrefHeight(hSplitPane.getPrefHeight());
        scrollPane.setPrefWidth(hSplitPane.getPrefWidth());

        if(treeView == null)
        {
            setTreeViewItems();
        }

        treeView.setPrefHeight(scrollPane.getHeight());
        treeView.setPrefWidth(scrollPane.getWidth());
    }

    public void initSetFocus()
    {
        scrollPane.requestFocus();
    }

}

Any help would be appreciated.

The TreeView isn't even showing anymore.

Updated code:

FXML

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<VBox maxHeight="720.0" maxWidth="1280.0" minHeight="720.0" minWidth="1280.0" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <children>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
      <children>
            <SplitPane dividerPositions="0.225" layoutX="155.0" layoutY="85.0" prefHeight="400.0" prefWidth="640.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <items>
                <AnchorPane maxWidth="300.0" minHeight="0.0" minWidth="300.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
                     <children>
                        <SplitPane fx:id="hSplitPane" dividerPositions="0.05" layoutX="14.0" layoutY="135.0" orientation="VERTICAL" prefHeight="718.0" prefWidth="285.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <items>
                            <AnchorPane maxHeight="34.0" minHeight="34.0" minWidth="300.0" prefHeight="100.0" prefWidth="160.0" SplitPane.resizableWithParent="false">
                                 <children>
                                    <TextField fx:id="searchBar" layoutX="75.0" prefHeight="33.0" prefWidth="980.0" promptText="Search..." AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="0.0">
                                       <font>
                                          <Font size="15.0" />
                                       </font>
                                    </TextField>
                                 </children>
                              </AnchorPane>
                              <ScrollPane fx:id="scrollPane" hbarPolicy="NEVER" minWidth="300.0" prefHeight="677.0" prefWidth="283.0" vbarPolicy="ALWAYS" SplitPane.resizableWithParent="false">
                                 <content>
                                    <TreeView fx:id="treeView" fixedCellSize="1.0" />
                                 </content>
                              </ScrollPane>
                          </items>
                        </SplitPane>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="980.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <TabPane fx:id="tabPane" layoutX="411.0" layoutY="244.0" prefHeight="718.0" prefWidth="987.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
      </children>
    </AnchorPane>
  </children>
</VBox>

Controller

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;

public class PokeBaseGUIController
{
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ScrollPane scrollPane;

    @FXML
    private SplitPane hSplitPane;

    @FXML
    private TreeView<String> treeView;

    @FXML
    private AnchorPane treeViewPane;

    @FXML
    private TextField searchBar;

    @FXML
    private TabPane tabPane;

    @FXML
    void initialize()
    {
        assert scrollPane != null : "fx:id=\"scrollPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert hSplitPane != null : "fx:id=\"hSplitPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert treeView != null : "fx:id=\"treeView\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert searchBar != null : "fx:id=\"searchBar\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";
        assert tabPane != null : "fx:id=\"tabPane\" was not injected: check your FXML file 'PokeBaseGUI.fxml'.";

        setTreeViewSize();
        setTreeViewItems();
    }

    private void setTreeViewItems()
    {
        TreeItem<String> pokeBaseItem = new TreeItem<String> ("PokeBase");
        pokeBaseItem.setExpanded(true);

        TreeItem<String> pokemonItems = new TreeItem<String> ("Pokemon");
        TreeItem<String> abilitiesItem = new TreeItem<String> ("Abilities");
        TreeItem<String> movesItem = new TreeItem<String> ("Moves");
        TreeItem<String> typesItem = new TreeItem<String> ("Types");

        pokeBaseItem.getChildren().add(pokemonItems);
        pokeBaseItem.getChildren().add(abilitiesItem);
        pokeBaseItem.getChildren().add(movesItem);
        pokeBaseItem.getChildren().add(typesItem);

        treeView.setRoot(pokeBaseItem);
    }

    public String getSearchText()
    {
        return searchBar.getText();
    }

    public void setTreeViewSize()
    {
        scrollPane.setPrefHeight(hSplitPane.getPrefHeight());
        scrollPane.setPrefWidth(hSplitPane.getPrefWidth());

        if(treeView == null)
        {
            setTreeViewItems();
        }

        treeView.setPrefHeight(scrollPane.getHeight());
        treeView.setPrefWidth(scrollPane.getWidth());
    }

    public void initSetFocus()
    {
        scrollPane.requestFocus();
    }

}

Edited 2 Years Ago by Doogledude123

This article has been dead for over six months. Start a new discussion instead.