【问题标题】:JavaFX - List of custom controls inside another custom control using FXMLJavaFX - 使用 FXML 的另一个自定义控件中的自定义控件列表
【发布时间】:2018-04-10 02:59:54
【问题描述】:

我想创建一个自定义控件,我可以在其中设置另一个自定义控件的列表,并且我希望能够像在 JavaFX TableView 中那样使用 FXML(请参阅列列表):

JavaFX 表格视图

TableView.fxml

<TableView fx:id="tableView">

    <columns>

        <TableColumn>...</TableColumn>

        <TableColumn>...</TableColumn>

      ...
    </columns>
</TableView>

TableView.java

public class FXMLTableViewController {

    @FXML TableView<MyBean> tableView;

    private void myMethod1() {
        ObservableList<TableColumn<MyBean, ?>> columns = tableView.getColumns();
        ...

    }
}

我只想写这样的东西:

自定义控件1

CustomControl1.fxml

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

CustomControl1.java

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();
        ...
    }
}

我已经知道如何实现简单的自定义控件,但是我还没有发现任何关于以这种方式编写自定义控件的信息。你能给我指点方向吗?

【问题讨论】:

    标签: java javafx custom-controls fxml observablelist


    【解决方案1】:

    您应该在&lt;fx:root&gt; 元素中使用类的真实类型,因为Control 不提供customList。 IE。使用type="my.package.CustomControl1"之类的东西。

    此外,以TableView 的方式添加元素允许您为周围标签创建的类需要提供带有标签名称的只读列表属性,即

    public class CustomControl1 extends Control {
    
        ...
    
        public ObservableList<CustomControl2> getCustomList() {
            return controls2;
        }
    
    }
    

    【讨论】:

    • 那么 CustomControl1 类将同时是 fx:root 元素的类型和控制器?这是允许的,因为类正在定义这个元素?
    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多