gpt4 book ai didi

JavaFX 如何保存 TableView 的状态

转载 作者:搜寻专家 更新时间:2023-11-01 03:33:52 26 4
gpt4 key购买 nike

我正在开发一个 JAVAFX 应用程序。在我的应用程序中,单击一个按钮后,它会打开一个窗口,其中有一个 TableView 以及一个 ApplySave 按钮。单击 Apply 按钮时,它将保留 TableView 的当前状态(以防我们添加/删除表行并单击应用并重新打开之前更新的表TableView 应显示)。 Save 按钮用于将表格记录保存到数据库中。假设表中有两行(来自数据库),如果我添加第 3 行并单击 Apply,我的 TableView 窗口将关闭。如果我重新打开表格,第三行将不存在。

如何保留之前添加的第三行,而不将其插入数据库?

最佳答案

您可以尝试将 TableView 的项目(或者可选地只存储 TableView 的添加项目的列表)作为打开窗口。

我创建了一个示例:

应用程序 TableViewSample 可用于打开第二个窗口。此应用程序存储 TablePopUp 的实例,该类可以显示第二个模态 Stage,同时维护一个“缓冲区” - Person 列表(数据模型显示在 TableView) 对象上,添加到 TableView 并“应用”但尚未存储在数据库中。

public class TableViewSample extends Application {

// Stores the state of the TableView and opens the second window
TablePopUp popUp = new TablePopUp();

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) {
Scene scene = new Scene(new BorderPane());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);

BorderPane root = (BorderPane) scene.getRoot();

Button button = new Button("Open window");
button.setOnAction((e) -> popUp.showTable());

root.setCenter(button);

stage.setScene(scene);
stage.show();
}

class TablePopUp {

// Stores the Person object which were added and applied but not stored
// in DB
ObservableList<Person> bufferAdd = FXCollections.observableArrayList();




// Simulate the items coming from the DV
private ObservableList<Person> dataFromDB = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com"));

void showTable() {
// Temporary buffer for the added Persion objects
ObservableList<Person> tempBuffer = FXCollections.observableArrayList();
// Temporary buffer to store persons to be deleted on apply
ObservableList<Person> bufferRemoveFromBuffer = FXCollections.observableArrayList();
// Data what the TableView displays
ObservableList<Person> tableData = FXCollections.observableArrayList();
// Stores the person objects that will be removed from the DB if Save is pressed
ObservableList<Person> bufferRemoveFromDB = FXCollections.observableArrayList();

// The Table displays elements from the DB + the applied buffer
tableData.addAll(dataFromDB);
tableData.addAll(bufferAdd);

// Create the table
TableView<Person> table = new TableView<Person>();
table.setItems(tableData);

Scene scene = new Scene(new BorderPane());
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));

TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));

table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

TextField addFirstName = new TextField();
addFirstName.setPromptText("First Name");
addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
TextField addLastName = new TextField();
addLastName.setMaxWidth(lastNameCol.getPrefWidth());
addLastName.setPromptText("Last Name");
TextField addEmail = new TextField();
addEmail.setMaxWidth(emailCol.getPrefWidth());
addEmail.setPromptText("Email");

// Button to add a new Person
Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {

Person newPerson = new Person(addFirstName.getText(), addLastName.getText(), addEmail.getText());

// Add a new element to the temporary buffer and add it to
// the table data also
tempBuffer.add(newPerson);
tableData.add(newPerson);
addFirstName.clear();
addLastName.clear();
addEmail.clear();
}
});


// Button to remove a Person
Button removeButton = new Button("Remove");
removeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {

Person selectedItem = table.getSelectionModel().getSelectedItem();

if(selectedItem != null) {

// Remove the item from the list of the displayed persons
tableData.remove(selectedItem);

// Check the buffers: if one of the buffer contains the selected item, remove it from the buffer
if(tempBuffer.contains(selectedItem))
tempBuffer.remove(selectedItem);
else if(bufferAdd.contains(selectedItem))
bufferRemoveFromBuffer.add(selectedItem);
else {
// The item is not in the buffers -> remove the item from the DB
bufferRemoveFromDB.add(selectedItem);
}
}
}
});

HBox hb = new HBox();
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, removeButton);
hb.setSpacing(3);

VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);

BorderPane root = (BorderPane) scene.getRoot();
root.setCenter(vbox);

Stage stage = new Stage();

HBox applySave = new HBox();

// On Save:
// Remove all elements from the buffer that were selected to be deleted
// Remove all elements from the BD that were selected to be deleted
// Add all the elements from the persistent buffer to the DB
// Add all the elements from the temporary buffer to the DB
// Clear both buffers
Button saveButton = new Button("Save to DB");
saveButton.setOnAction((e) -> {
bufferAdd.removeAll(bufferRemoveFromBuffer);
dataFromDB.removeAll(bufferRemoveFromDB);


dataFromDB.addAll(bufferAdd);
dataFromDB.addAll(tempBuffer);

bufferAdd.clear();
stage.close();
});

// On Apply:
// Add elements from the temporary buffer to the persistent buffer
// Remove elements from the buffer
Button applyButton = new Button("Apply");
applyButton.setOnAction((e) -> {
bufferAdd.addAll(tempBuffer);

bufferAdd.removeAll(bufferRemoveFromBuffer);

stage.close();
});

applySave.getChildren().addAll(saveButton, applyButton);

root.setBottom(applySave);

stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);
stage.show();
}
}

public static class Person {

private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}

public String getFirstName() {
return firstName.get();
}

public void setFirstName(String fName) {
firstName.set(fName);
}

public String getLastName() {
return lastName.get();
}

public void setLastName(String fName) {
lastName.set(fName);
}

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
}
}

TablePopUp 类实际上有两个缓冲区:一个是临时缓冲区,用于存储添加的元素,另一个是持久缓冲区,它保存在不同的窗口开口之间。如果按下“应用”按钮,临时缓冲区将存储在持久缓冲区中。如果按下“保存”按钮,两个缓冲区都将存储在数据库中,然后它们将被清除。

在示例中,删除操作也被缓冲。删除时会发现,选择要删除的 Person 对象是否来自数据库。如果它来自数据库,它会被放入一个缓冲区,并且只有在按下保存按钮时才会从数据库中删除。相同的工作流程对已添加但尚未放入数据库的人员有效:删除时,如果按下应用按钮,他们只会被删除。

关于JavaFX 如何保存 TableView 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38345317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com