你只需要在表上調用getItems()
並遍歷返回的列表。假設所有東西都以普通的「JavaFX方式」設置,列表元素中的任何屬性都將被編輯機制自動更新。
這是一個完整的基於FXML的示例。
EditableTableExample.fxml(在包application
):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.cell.TextFieldTableCell?>
<?import javafx.scene.layout.HBox?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<BorderPane xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.EditableTableViewController">
<center>
<TableView fx:id="table" editable="true">
<columns>
<TableColumn text="First Name" prefWidth="150"
fx:id="firstNameColumn">
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn" />
</cellFactory>
</TableColumn>
<TableColumn text="Last Name" prefWidth="150" fx:id="lastNameColumn">
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn" />
</cellFactory>
</TableColumn>
<TableColumn text="Email" prefWidth="150" fx:id="emailColumn">
<cellFactory>
<TextFieldTableCell fx:factory="forTableColumn" />
</cellFactory>
</TableColumn>
</columns>
</TableView>
</center>
<bottom>
<HBox alignment="CENTER">
<padding>
<Insets top="10" right="10" left="10" bottom="10" />
</padding>
<children>
<Button onAction="#showData" text="Show Data" />
</children>
</HBox>
</bottom>
</BorderPane>
EditableTableController:
package application;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class EditableTableViewController {
@FXML
private TableView<Person> table ;
@FXML
private TableColumn<Person, String> firstNameColumn ;
@FXML
private TableColumn<Person, String> lastNameColumn ;
@FXML
private TableColumn<Person, String> emailColumn ;
public void initialize() {
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
emailColumn.setCellValueFactory(cellData -> cellData.getValue().emailProperty());
table.getItems().addAll(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
) ;
}
@FXML
private void showData() {
for (Person person : table.getItems()) {
String formatted = String.format("%s %s (%s)", person.getFirstName(), person.getLastName(), person.getEmail());
System.out.println(formatted);
}
System.out.println();
}
}
Model類Person
:
package application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
public final String getFirstName() {
return firstNameProperty().get();
}
public final void setFirstName(String firstName) {
firstNameProperty().set(firstName);
}
public StringProperty firstNameProperty() {
return firstName ;
}
private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
public final String getLastName() {
return lastNameProperty().get();
}
public final void setLastName(String lastName) {
lastNameProperty().set(lastName);
}
public StringProperty lastNameProperty() {
return lastName ;
}
private final StringProperty email = new SimpleStringProperty(this, "email");
public final String getEmail() {
return emailProperty().get();
}
public final void setEmail(String email) {
emailProperty().set(email);
}
public StringProperty emailProperty() {
return email ;
}
public Person(String firstName, String lastName, String email) {
this.setFirstName(firstName);
this.setLastName(lastName);
this.setEmail(email);
}
}
應用類:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class EditableTableViewExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("EditableTableExample.fxml"));
BorderPane root = loader.load();
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
該按鈕僅供演示,但按下該按鈕將調用控制器中的showData()
方法,該方法遍歷表的項目列表並從每個項目中檢索屬性值。如果雙擊一個要編輯的單元格,鍵入以更改該值,然後使用提交編輯輸入,然後按按鈕將顯示更新的值。