是否可以在javaFX节点中存储对象的实例?

Ada*_*ger 1 java tabs javafx fxml

我有一个JavaFX应用程序,从主窗口开始作为一个TabPane对象。在那个TabPane我有一个特定的Tab,其中包含一个Button对象,可以将新的选项卡添加到main TabPane。但我需要每个缠绕Tab在主TabPane用一个对象实例(每个标签应使用类的一个实例工作Merchant

该类Merchant具有一些方法createSortiment(),可以ArrayList根据Merchant对象的参数生成随机选择的项目。

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

我需要能够添加一个新选项卡(我可以这样做)并将其绑定到。Merchant JohnSmith = new Merchant();然后,我需要控制器GUIMerchantTabController能够fx:id="createSortiment"通过调用来响应按钮的动作事件JohnSmith.createSortiment()(我不知道如何做到这一点),并且每个产生的项目添加到一些AccordionTab作为TitledPane(我还可以做)。

我的主要问题:

我如何将JohnSmith的实例保存到TabGUIMerchantTabController将能够使用John的方法并访问他的数据的特定位置?可以引用某种对象实例吗?我可以以某种方式将对象作为节点添加到该窗格中吗?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>
Run Code Online (Sandbox Code Playgroud)

主窗口的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>
Run Code Online (Sandbox Code Playgroud)

jew*_*sea 5

节点具有一个userData API:

public void setUserData(Object value) 
Run Code Online (Sandbox Code Playgroud)

设置单个Object属性的便利方法,以后可以检索该属性。这在功能上等效于调用该getProperties().put(Object key, Object value)方法。以后可以通过调用来检索此内容 getUserData()

它还具有用于关联键-值对映射的属性API:

public final ObservableMap<Object,Object> getProperties()
Run Code Online (Sandbox Code Playgroud)

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

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

或者,您可以扩展使用的任何节点类型(只要不是最终类型,很多事情都是最终的,并且在JavaFX中用户不可扩展),然后在扩展中存储引用。例如:

class MyTab extends Tab {
    private MyClass myObject;

    public MyTab(MyClass myObject) {
        super();
        this.myObject = myObject;
    }
}
Run Code Online (Sandbox Code Playgroud)

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