将自定义组件拉伸到完整大小

时间:2018-12-06 09:00:58

标签: java javafx fxml

我已经创建了一个用于可视化动态表的自定义组件。它具有以下FXML。它被另一个FXML使用。所提供的控制器不会对布局进行任何更改,因此我不在此处发布它们。

现在,当我启动应用程序时,它们应该重新调整为父级的大小。但是他们没有:

enter image description here

Component本身会调整为正确的布局范围,而子级BorderPane则无法按原样显示:

结构:

enter image description here

DynTableComponent:

enter image description here

DynTableComponent的子项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:

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

有人可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

解决方案1 ​​

我找到了解决方法。 而不是使用自定义组件,我将FXML包含在其他fxml中,如下所示:

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

解决方案2:

因此,现在我将组件定义为https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm#BABDAAHE中提到的fx:root并修改了控制器。现在可以了。