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>