0

I'm newbie in Java FXML. I want to add an item to the TableView in first stage(tableview.fxml) from form I've created in second stage(dodawanie.fxml). Can someone tell me how to do this?

Calling the method AddPersonToTable from DodawanieController causes that ObservableList data is updated, but the TableView not.

TableViewController.java

public class TableViewController {

    @FXML public TableView<Person> tableView;
    @FXML private TextField firstNameField;
    @FXML private TextField lastNameField;
    @FXML private TextField emailField;

    @FXML
    protected void addPerson(ActionEvent event) throws IOException {

        Parent root = FXMLLoader.load(getClass().getResource("Dodawanie.fxml"));
        Stage stage = new Stage();
        stage.setTitle("Dodaj ksiazke");
        stage.setScene(new Scene(root));
        stage.show();
    }

    public void updateTable(Person person){
        ObservableList<Person> data = tableView.getItems();
        data.add(person);
        tableView.setItems(data);
    }

}

DodawanieController.java

public class DodawanieController {

    @FXML
    private TextField firstNameField;

    @FXML
    private TextField lastNameField;

    @FXML
    private TextField emailField;

    @FXML
    public void AddPersonToTable(ActionEvent actionEvent) throws IOException {
        Person person = new Person(firstNameField.getText(), lastNameField.getText(), emailField.getText());
        System.out.println(person);

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("tableview.fxml"));
        loader.load();
        TableViewController tableViewController = loader.getController();
        tableViewController.updateTable(person);
    }
}

tableview.fxml

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.TableViewController">
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
    <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" GridPane.columnIndex="0" GridPane.rowIndex="0">
    </Label>
    <TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1">
        <columns>
            <TableColumn text="First Name">
                <cellValueFactory><PropertyValueFactory property="firstName" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Last Name">
                <cellValueFactory><PropertyValueFactory property="lastName" />
                </cellValueFactory>
            </TableColumn>
            <TableColumn text="Email Address">
                <cellValueFactory><PropertyValueFactory property="email" />
                </cellValueFactory>
            </TableColumn>

        </columns>
        <items>
            <FXCollections fx:factory="observableArrayList">
                <Person email="jacob.smith@example.com" firstName="Jacob" lastName="Smith" />
                <Person email="isabella.johnson@example.com" firstName="Isabella" lastName="Johnson" />
                <Person email="ethan.williams@example.com" firstName="Ethan" lastName="Williams" />
                <Person email="emma.jones@example.com" firstName="Emma" lastName="Jones" />
                <Person email="michael.brown@example.com" firstName="Michael" lastName="Brown" />
            </FXCollections>
        </items>
    </TableView>
    <Button alignment="CENTER_RIGHT" onAction="#addPerson" text="Add" GridPane.rowIndex="2" />
   <HBox prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="3">
      <children>
         <TextField fx:id="firstNameField" />
         <TextField fx:id="lastNameField" />
         <TextField fx:id="emailField" />
      </children>
   </HBox>
   <Button onAction="#addPerson2" mnemonicParsing="false" text="Dodaj" GridPane.rowIndex="4" />
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
</GridPane>

Dodawanie.fxml

<GridPane alignment="CENTER" hgap="10.0" prefHeight="400" prefWidth="600" vgap="10.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.DodawanieController">
   <columnConstraints>
      <ColumnConstraints />
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0" spacing="10">
         <children>
            <Label text="First Name" />
            <Label text="Last Name" />
            <Label text="Email" />
         </children>
      </VBox>
      <VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1" spacing="10">
         <children>
            <TextField fx:id="firstNameField" />
            <TextField fx:id="lastNameField" />
            <TextField fx:id="emailField" />
            <Button mnemonicParsing="false" text="Button" onAction="#AddPersonToTable"/>
         </children>
      </VBox>
   </children>

</GridPane>
AndroideuszPL
  • 385
  • 1
  • 2
  • 13
  • One thing you can do is learn MVC. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 Sep 12 '19 at 19:49
  • https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – SedJ601 Sep 12 '19 at 19:50
  • 1
    (Ignoring best practice) the problem here is that you load a new version of the scene containing the `TableView` instead of using the one that is already shown on screen. You're modifying a scene that is never shown on screen. The quick and dirty fix here would be doing the "passing of the data" the other way round: Pass e.g. the `TableViewController` to the `DodawanieController` when loading `Dodawanie.fxml`. Btw:You do not need to reassign the ObservableList to the `items` property: Since modifications are done through the ObservableList the listener `TableView` added to the list is notified – fabian Sep 12 '19 at 20:27
  • 1
    Unrelated to your problem: please learn java naming conventions and stick to them. – kleopatra Sep 13 '19 at 09:27

0 Answers0