Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX - TabPane/Tab How to add content which fills the tab in size?

Tags:

tabs

javafx


I am playing with JavaFX and i have a small problem, which i cannot solve.
I want to add a new Tab (from FXML) to a existing TabPane.

This is my code:

try
{
   URL location = WPClientController.class.getResource("WPClient.fxml"); 
   FXMLLoader fxmlLoader = new FXMLLoader();
   fxmlLoader.setLocation(location);
   fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
   Node node = (Node) fxmlLoader.load(location.openStream());

   Tab newTab = new Tab("engel", node);
   fxTabWP.getTabs().add(newTab);
}
catch (Exception exception)
{
  //no real error handling...just print the stacktrace... 
  exception.printStackTrace();
}

this code will result in:

enter image description here

The code works, but (as seen on the screenshot) the added node (a SplitPane here) uses it's preferred size (on TabPage, which is wrong for me).
The size of the added SplitPane should have the same size as it's parent container. (and it should grow and shrink, when the parent container grows or shrinks) (like BorderLayout in Swing)

EDIT (here is [now the correct] content of the TAB FXML):

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

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

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.test.ui.fx.client.ClientController">
 <children>
  <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
     <children>
        <SplitPane dividerPositions="0.29797979797979796" prefHeight="160.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
          <items>
              <HBox prefHeight="100.0" prefWidth="200.0">
                 <children>
                    <TreeView fx:id="fxTreeView" prefHeight="200.0" prefWidth="200.0" HBox.hgrow="ALWAYS" />
                 </children>
              </HBox>
              <HBox fx:id="fxTreeNodeContentBox" prefHeight="100.0" prefWidth="200.0" />
          </items>
        </SplitPane>
     </children>
    </HBox>
 </children>
</VBox>

My question: What is todo, so the SplitPane's size will dynamically fit it's parent container size?

like image 224
Ben Avatar asked Dec 07 '25 22:12

Ben


1 Answers

Your FXML file has a root element with

<VBox maxHeight="-Infinity" maxWidth="-Infinity" ... >

The maxHeight and maxWidth attributes are preventing it from growing beyond its preferred size. (In addition, the preferred sizes of its child elements are, for some reason, set to fixed values.) If you remove these attributes it will allow the root element to grow as needed.

like image 55
James_D Avatar answered Dec 10 '25 02:12

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!