我可以将对象的实例存储在Javafx节点中吗?



我有一个Javafx应用程序,从主窗口开始为TabPane对象。在TabPane中,我有一个特定的Tab,其中包含一个Button对象,可以将新选项卡添加到主TabPane。但是我需要将主TabPane中的每个Tab与单个对象实例纠缠(每个选项卡都应与类Merchant的一个实例一起使用)

Merchant具有一些createSortiment()的方法,该方法基于Merchant对象的参数生成随机选择项目的ArrayList

主控制器GUIController控制主窗口,另一个控制器GUIMerchantTabController控制选项卡。

我需要能够添加一个新选项卡(我可以这样做)并以某种方式将其绑定到Merchant JohnSmith = new Merchant();,然后我需要控制器GUIMerchantTabController才能使用fx:id="createSortiment"响应按钮的操作事件,并调用JohnSmith.createSortiment()(我不知道该怎么做)并且将每个生成的项目添加到Tab中的某些Accordion作为TitledPane(我也可以做)。

我的主要问题:

如何将约翰史密斯的实例保存到GuimerchantTabController可以使用John的方法并访问他的数据的特定Tab中?是否可能某种对象实例refference?我可以以某种方式将对象添加到该窗格作为节点吗?Java中是否存在一些"数据"属性(如HTML <element data-storeSomething="Some text here, or json object">中)?

我认为,查看我的文件不是必需的,而是为了更好的想法,这些是我的fmxls ...

商人选项卡的FXML:

<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.text.Font?>

<Tab fx:controller="damn.fuck.shit.GUIMerchantTabController" closable="false" text="Merchant name" xmlns:fx="http://javafx.com/fxml/1">
    <content>
        <AnchorPane prefHeight="200.0" prefWidth="200.0">
            <children>
                <SplitPane dividerPositions="0.7508361204013378" prefHeight="371.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <items>
                        <ScrollPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                            <Accordion fx:id="sortimentViewSpace" prefHeight="369.0" prefWidth="438.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                                <panes>
                                    <TitledPane animated="false" text="Item00">
                                        <content>
                                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                                                <Label text="This is testing item label lol" />
                                            </AnchorPane>
                                        </content>
                                    </TitledPane>
                                </panes>
                            </Accordion>
                        </ScrollPane>
                        <ScrollPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                            <Button fx:id="createSortimentButton" layoutX="51.0" layoutY="-338.0" mnemonicParsing="false" onAction="#generateSortiment" prefHeight="31.0" prefWidth="142.0" text="Vytvoř sortiment" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                                <font>
                                    <Font name="System Bold" size="14.0" />
                                </font>
                            </Button>
                        </ScrollPane>
                    </items>
                </SplitPane>
            </children>
        </AnchorPane>
    </content>
</Tab>

主窗口的FXML:

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.cell.ChoiceBoxListCell?>
<TabPane fx:id="mainWindow" maxHeight="400.0" maxWidth="600.0" minHeight="400.0" minWidth="600.0" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="damn.fuck.shit.GUIController">
  <tabs>
    <Tab closable="false" text="Volby">
         <content>
            <Pane prefHeight="200.0" prefWidth="200.0">
               <children>
                  <FlowPane hgap="5.0" layoutX="0.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="371.0" prefWidth="300.0" vgap="5.0">
                     <children>
                        <Label alignment="CENTER" prefHeight="25.0" prefWidth="307.0">
                            <font>
                                <Font name="System Bold" size="14.0" />
                            </font>
                            <text>
                                    Vytvoř nového kupce
                            </text>
                        </Label>
                         <Label prefHeight="25.0" prefWidth="40.0" text="Jméno:" />
                         <TextField prefHeight="25.0" prefWidth="235.0" promptText="Vepiš jméno nového kupce" />
                        <CheckBox mnemonicParsing="false" prefHeight="25.0" prefWidth="115.0" text="Prodává magicé předměty" />
                         <Button fx:id="createMerchantButton" prefHeight="25.0" prefWidth="150.0" text="Vytvoř nového kupce" onAction="#addMerchantTab">
                         </Button>
                     </children>
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                     <padding>
                        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                     </padding>
                  </FlowPane>
               </children>
            </Pane>
         </content></Tab>
  </tabs>
</TabPane>

节点具有userData api:

public void setUserData(Object value) 

设置的便利方法 可以在以后检索的单个对象属性。这是 在功能上等同于调用getProperties().put(Object key, Object value)方法。以后可以通过致电来检索 getUserData()

它还具有用于关联键 ->值对的地图的属性API:

public final ObservableMap<Object,Object> getProperties()

返回该节点上可观察的属性图主要由应用程序开发人员使用。

这两个API都允许您将用户对象实例与Javafx节点相关联。

另外,您可以扩展所使用的任何节点类型(只要它不是最终的,许多内容是最终的,而不是用户在Javafx中扩展),并在扩展程序中存储一个参考。例如:

class MyTab extends Tab {
    private MyClass myObject;
    public MyTab(MyClass myObject) {
        super();
        this.myObject = myObject;
    }
}

使用类扩展名具有保留类型信息的优点,以防您的应用程序受益。

最新更新