Search code examples
javajavafxfxml

Stretching Custom Component to Full Size


I've created a custom Component for visualizing a Dynamic Table. It has the Following FXML. It get's used by another FXML. The provided controllers do not make any change on the layout, so I don't post them here.

Now when I start the Application they should resize to the size of Parent. But they don't:

enter image description here

The Component itself resizes to the correct layout bounds, while the children BorderPane don't as it is can be seen here:

Structure:

enter image description here

DynTableComponent:

enter image description here

DynTableComponent's children BorderPane:

enter image description here

FXML:

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import org.controlsfx.control.textfield.CustomTextField?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.fc.gui.general.DynTableController">
    <center>
        <TableView BorderPane.alignment="CENTER" />
    </center>
    <top>
        <BorderPane BorderPane.alignment="CENTER">
            <right>
                <HBox>
                    <children>
                        <JFXButton onAction="#buttonCloseSearchClicked" styleClass="buttonSearchClose">
                            <graphic>
                                <FontAwesomeIconView styleClass="buttonSearchCloseIcon" />
                            </graphic>
                        </JFXButton>

                        <CustomTextField styleClass="searchField">
                            <left>
                                <Label styleClass="searchBoxLabel">
                                    <graphic>
                                        <FontAwesomeIconView styleClass="searchBoxLabelIcon" />
                                    </graphic>
                                </Label>
                            </left>
                        </CustomTextField>
                    </children>
               <BorderPane.margin>
                  <Insets right="10.0" top="10.0" />
               </BorderPane.margin>
                </HBox>

            </right>
        </BorderPane>
    </top>
    <bottom>
        <BorderPane maxHeight="40.0" prefHeight="40.0" prefWidth="600.0" BorderPane.alignment="CENTER">
            <right>
                <HBox alignment="CENTER_LEFT" maxHeight="40.0" prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
                    <children>
                        <Label text="Label" />
                    </children>
                </HBox>
            </right>
        </BorderPane>
    </bottom>
</BorderPane>

DynTableComponent:

public class DynTableComponent extends BorderPane
{
    private Node view;
    private DynTableController controller;

    public DynTableComponent()
    {
        super();
        FXMLLoader fxmlLoader = new FXMLLoader(
                getClass().getClassLoader().getResource("gui/fxml/general/DynTable.fxml"));
        fxmlLoader.setControllerFactory(new Callback<Class<?>, Object>()
        {
            @Override
            public Object call(Class<?> param)
            {
                return controller = new DynTableController();
            }
        });
        try
        {
            view = (Node) fxmlLoader.load();

        } catch (IOException ex)
        {
        }

        getChildren().add(view);
    }

    public void init(Class<? extends DatabaseItem> item)
    {
        controller.initializeTable(item);
    }
}

FXML of Parent:

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

<?import com.jfoenix.controls.JFXButton?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import org.controlsfx.control.textfield.CustomTextField?>
<?import de.fc.gui.general.DynTableComponent?>


<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.fc.gui.base.AddressViewController">
   <center>
      <DynTableComponent fx:id="dynTable"/>
   </center>
</BorderPane>

Does anyone have a solution for this problem?


Solution

  • Solution 1

    I've found a workaround to this. Instead of using a custom Component I'm including the FXML inside the other fxml like this:

    <BorderPane>
       <center>
          <fx:include source="../general/DynTable.fxml" />
       </center>
    </BorderPane>
    

    Solution 2:

    So now I defined my component as fx:root as it is mentioned in https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm#BABDAAHE and modified the controller. Now it works.