[英]JavaFX - List of custom controls inside another custom control using FXML
我想創建一個自定義控件,我可以在其中設置另一個自定義控件的列表,並且我希望能夠像在 JavaFX TableView 中一樣使用 FXML(請參閱列列表):
<TableView fx:id="tableView">
<columns>
<TableColumn>...</TableColumn>
<TableColumn>...</TableColumn>
...
</columns>
</TableView>
public class FXMLTableViewController {
@FXML TableView<MyBean> tableView;
private void myMethod1() {
ObservableList<TableColumn<MyBean, ?>> columns = tableView.getColumns();
...
}
}
我只想寫一些類似的東西:
<?import javafx.scene.control.*?>
<fx:root type="javafx.scene.control.Control" xmlns:fx="http://javafx.com/fxml">
<customList>
<CustomControl2>
...
</CustomControl2>
<CustomControl2>
...
</CustomControl2>
...
</customList>
</fx:root>
public class CustomControl1 extends Control {
private ObservableList<CustomControl2> controls2;
public CustomControl1() {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("CustomControl1.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
private void myMethod1() {
controls2 = getControls2();
...
}
}
我已經知道如何實現簡單的自定義控件,但我還沒有找到任何關於以這種方式組合自定義控件的信息。 你能給我指點方向嗎?
您應該在<fx:root>
元素中使用類的真實類型,因為Control
不提供customList
。 即使用類似type="my.package.CustomControl1"
東西。
此外,以TableView
允許您為周圍標簽創建的類的方式添加元素需要提供帶有標簽名稱的只讀列表屬性,即
public class CustomControl1 extends Control {
...
public ObservableList<CustomControl2> getCustomList() {
return controls2;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.