【问题标题】:JavaFX custom component usageJavaFX 自定义组件使用
【发布时间】:2015-08-27 20:49:51
【问题描述】:

我在 JavaFX 中的自定义组件有问题。

我的自定义组件包含包含 TableView 和 TabPane 的拆分窗格:

MasterDetail.fxml

<fx:root type="javafx.scene.layout.VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <SplitPane dividerPositions="0.5" orientation="VERTICAL">
            <items>
                <AnchorPane>
                    <children>
                        <TableView fx:id="table"/>
                    </children>
                </AnchorPane>

                <AnchorPane>
                    <children>
                        <TabPane fx:id="tabPane"/>
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
    </children>
</fx:root>

我想在另一个 fxml 中使用我的自定义控件,所以我创建了它,以及如何在我的 MasterDetail 组件中为 TableView 声明列?

例如我想做这样的事情:

<MasterDetail fx:id="masterDetail">
    <table>
        <columns>
            <TableColumn fx:id="nameColumn" prefWidth="75.0" text="Name"/>
            <TableColumn fx:id="createDateColumn" prefWidth="75.0" text="Create date"/>
        </columns>
    </table>
</MasterDetail>

在 fxml 中可以做到这一点吗?

【问题讨论】:

    标签: java javafx javafx-8 fxml


    【解决方案1】:

    可以在“masterDetail”组件中定义表格列的数量。但要发生这种情况,我们必须定义读取表格列的自定义属性。

    例子:

       <CustomeTable>
            <children>
              <TableView fx:id="table"/>
            </children>
      </CustomeTable>
    

    定义扩展 AnchorPane 的 CustomeTable。

    public class CustomeTable extends AnchorPane{
    
        private ObjectProperty<Integer> noOfColumns = new SimpleObjectProperty<Integer>();
    
    public void setNoOfColumns(Integer noOfColumns){
    noOfColumns.set(noOfColumns);    
    }
    
    public Integer getNoOfColumns(){
    return noOfColumns.get();
    }
    
    public CustomeTable(){
    
    // add the listener to the ObjectProperty
    // which updates the noOfColumns into table
    noOfCoulmns.addListener();
    
    }
    
    
    }
    

    【讨论】:

    • 谢谢,但我想创建一个包含表格的自定义组件 (MasterDetial.fxml)。后来我想在 fxml 中使用这个组件,我想为在 MasterDetail.fxml 中定义的表定义列。例如在 MasterDetail.fxml 中,我有一个没有任何列的空表,后来我想创建另一个使用组件的 fxml 并将列添加到我在 MasterDetail 中定义的表中。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    相关资源
    最近更新 更多