将自定义组件拉伸到全尺寸
Stretching Custom Component to Full Size
我创建了一个自定义组件来可视化动态 Table。它具有以下 FXML。它被另一个 FXML 使用。提供的控制器不会对布局进行任何更改,因此我不会在此处 post 它们。
现在,当我启动应用程序时,它们应该调整为父级的大小。但他们没有:
组件本身会调整到正确的布局边界,而子 BorderPane 不会按原样调整,请参见此处:
结构:
动态Table分量:
DynTable组件的子 BorderPane:
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>
动态Table分量:
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
我找到了解决方法。
我没有使用自定义组件,而是将 FXML 包含在另一个 fxml 中,如下所示:
<BorderPane>
<center>
<fx:include source="../general/DynTable.fxml" />
</center>
</BorderPane>
方案二:
所以现在我将我的组件定义为fx:root,正如https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm#BABDAAHE中提到的那样,并修改了控制器。现在可以了。
我创建了一个自定义组件来可视化动态 Table。它具有以下 FXML。它被另一个 FXML 使用。提供的控制器不会对布局进行任何更改,因此我不会在此处 post 它们。
现在,当我启动应用程序时,它们应该调整为父级的大小。但他们没有:
组件本身会调整到正确的布局边界,而子 BorderPane 不会按原样调整,请参见此处:
结构:
动态Table分量:
DynTable组件的子 BorderPane:
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>
动态Table分量:
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
我找到了解决方法。 我没有使用自定义组件,而是将 FXML 包含在另一个 fxml 中,如下所示:
<BorderPane>
<center>
<fx:include source="../general/DynTable.fxml" />
</center>
</BorderPane>
方案二:
所以现在我将我的组件定义为fx:root,正如https://docs.oracle.com/javafx/2/fxml_get_started/custom_control.htm#BABDAAHE中提到的那样,并修改了控制器。现在可以了。