我一直在尝试将项目添加到ListView。这些项目是在弹出窗口中创建的,我想将它们添加到主窗口中的ListView。
我想从TextField获得一些文本,它应该将该文本添加到主窗口中的ListView。
我是JavaFX的新手,所以我一直只使用“Scene Builder”和FXMLDocumentController.java。在main.java中没有做过任何工作。
所以我需要帮助我如何在弹出窗口中将项目添加到主窗口中的ListView 在控制器文档中的方法。
(两个FXML都有相同的控制器)
public class FXMLDocumentController implements Initializable {
ObservableList<String> list = FXCollections.observableArrayList("mark", "tom");
@FXML
private ListView<String> itemList = new ListView<String>(list);
@FXML
private Button tilføjGin = new Button();
@FXML
private TextArea GinNoter = new TextArea();
@FXML
private TextField GinNavn = new TextField();
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
itemList.setItems(list);
}
public void gemGin(ActionEvent event) {
Gin g = new Gin(GinNavn.getText(), GinNoter.getText());
// list.add(g.getName()); <--
// itemList.setItems(list); <--
ginAdd(g); <--These dont work for me.
Stage stage2 = (Stage) tilføjGin.getScene().getWindow();
stage2.close();
}
public void ginAdd(Gin gin) {
list.add(gin.getName()); <--
itemList.setItems(list); <-- None of these seems to work.
// itemList.getItems().add(gin.getName()); <--
}
@FXML
public void createNewGin(ActionEvent event) throws IOException {
Stage stage = new Stage();
Parent root;
root = FXMLLoader.load(getClass().getResource("FXML-PopUp.fxml"));
<-- My pop-up window
//create a new scene with root and set the stage
stage.setScene(new Scene(root));
stage.setTitle("My modal window");
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
}
如果你想看我的FXML: 弹出窗口fxml:
<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="800.0"
xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="farginapp2.FXMLDocumentController">
<children>
<Label layoutX="25.0" layoutY="14.0" text="Ny Gin">
<font>
<Font name="System Bold Italic" size="26.0" />
</font>
</Label>
<Label layoutX="25.0" layoutY="72.0" text="Navn" />
<Label layoutX="25.0" layoutY="144.0" text="Årgang" />
<Label layoutX="25.0" layoutY="223.0" text="Oprindelse" />
<Label layoutX="25.0" layoutY="300.0" text="Alkohol %" />
<TextField fx:id="GinNavn" layoutX="25.0" layoutY="97.0"
promptText=""Gin Ginsen"" />
<TextField fx:id="GinAlder" layoutX="25.0" layoutY="170.0"
promptText=""10.000 BC"" />
<TextField fx:id="GinLand" layoutX="25.0" layoutY="247.0"
promptText=""Vatikanet"" />
<TextField fx:id="GinAlkohol" layoutX="25.0" layoutY="325.0"
prefHeight="121.0" prefWidth="172.0" promptText=""98%"" />
<Label layoutX="497.0" layoutY="52.0" text="Noter ">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
<Button fx:id="tilføjGin" layoutX="345.0" layoutY="529.0"
mnemonicParsing="false" onAction="#gemGin" prefHeight="49.0"
prefWidth="110.0"
text="Tilføj Gin">
<font>
<Font name="System Bold" size="19.0" />
</font>
</Button>
<TextArea fx:id="GinNoter" layoutX="483.0" layoutY="272.0"
prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
主窗口FXML:
<VBox prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="farginapp2.FXMLDocumentController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="CloseProgram" mnemonicParsing="false" onAction="#FileMenuAction1" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem fx:id="newGin" mnemonicParsing="false" onAction="#createNewGin" text="New Gin" />
<MenuItem mnemonicParsing="false" text="Delete Gin" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane dividerPositions="0.18485523385300667, 0.7527839643652561" prefHeight="607.0" prefWidth="900.0">
<items>
<AnchorPane prefHeight="542.0" prefWidth="149.0">
<children>
<ListView fx:id="itemList" layoutY="73.0" onMouseClicked="#listMouseAction" prefHeight="469.0" prefWidth="163.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="73.0" />
</children></AnchorPane>
<ScrollPane prefHeight="542.0" prefWidth="500.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="542.0" prefWidth="504.0">
<children>
<Button fx:id="BUTT_1" layoutX="226.0" layoutY="221.0" mnemonicParsing="false" onAction="#Butt1Action" text="CLICK MIG!" />
<Label fx:id="LabelMid" alignment="CENTER" layoutX="117.0" layoutY="47.0" prefHeight="140.0" prefWidth="271.0" text="Label" textAlignment="CENTER">
<font>
<Font name="System Bold" size="50.0" />
</font></Label>
</children></AnchorPane>
</content>
</ScrollPane>
<AnchorPane prefHeight="542.0" prefWidth="357.0">
<children>
<TextField fx:id="noter" layoutX="10.0" layoutY="50.0" prefHeight="480.0" prefWidth="200.0">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</TextField>
<Label layoutX="14.0" layoutY="3.0" prefHeight="47.0" prefWidth="82.0" text="Noter">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</children></AnchorPane>
</items>
</SplitPane>
<HBox prefHeight="94.0" prefWidth="900.0">
<children>
<Label prefHeight="17.0" prefWidth="41.0" text="Label" />
<Label alignment="CENTER_RIGHT" text="Label" />
</children>
</HBox>
</children>
</VBox>