我正在尝试将这个应用程序上的输入字段序列化为一个fxml文件,但我不知道怎么做。我知道如何序列化预定义的变量,例如:
user.setFirstName=("John");
但是如何序列化通过输入字段添加的信息呢?我希望能够在程序关闭时将添加的用户保存到xml文件中,并在下次启动时加载它们。由于您无法序列化ObservaleList,我尝试将其转换为arraylist,但没有成功。关于我在这里遗漏了什么有什么建议吗?
package test;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main6 extends Application {
private static final String SERIALIZED_FILE_NAME = "UserInformation.fxml";
Stage window;
TableView<User> table;
TextField firstNameInput, lastNameInput, ageInput;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("User Control");
TableColumn<User, String> firstNameColumn = new TableColumn<>("First Name");
firstNameColumn.setMinWidth(200);
firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn<User, String> lastNameColumn = new TableColumn<>("Last Name");
lastNameColumn.setMinWidth(200);
lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn<User, Integer> ageColumn = new TableColumn<>("Age");
ageColumn.setMinWidth(200);
ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(10, 10, 10, 10));
gridpane.setVgap(8);
gridpane.setHgap(10);
Label exception = new Label("");
GridPane.setConstraints(exception, 3, 3);
firstNameInput = new TextField();
firstNameInput.setPromptText("First name");
firstNameInput.setMinWidth(100);
GridPane.setConstraints(firstNameInput, 3, 0);
lastNameInput = new TextField();
lastNameInput.setPromptText("Last name");
lastNameInput.setMinWidth(100);
GridPane.setConstraints(lastNameInput, 4, 0);
ageInput = new TextField();
ageInput.setPromptText("Age");
ageInput.setMinWidth(100);
GridPane.setConstraints(ageInput, 5, 0);
Button addButton = new Button("Add");
addButton.setOnAction(e -> addButtonClicked());
GridPane.setConstraints(addButton, 4, 3);
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());
GridPane.setConstraints(deleteButton, 5, 3);
table = new TableView();
table.getColumns().addAll(firstNameColumn, lastNameColumn, ageColumn);
ArrayList<User> showing = new ArrayList<User>(table.getItems());
User user = new User();
XMLEncoder encoder = null;
try {
encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(SERIALIZED_FILE_NAME)));
System.out.println("File stream opened and XMLEncoder created");
} catch (FileNotFoundException fileNotFound) {
System.out.println("ERROR: While Creating or Opening the File");
}
encoder.writeObject(user);
encoder.flush();
encoder.close();
XMLDecoder decoder = null;
try {
decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(SERIALIZED_FILE_NAME)));
System.out.println("File stream opened and XMLDecoder created");
} catch (FileNotFoundException e) {
System.out.println("ERROR: File Student.xml not found");
}
// Creating new Student to demonstrate deserialization
// (Student) is a cast operation, indicating what class we want Student to be
User user2 = (User) decoder.readObject();
decoder.close();
window.setOnCloseRequest(e -> {
e.consume();
closeProgram();
});
gridpane.getChildren().addAll(firstNameInput, lastNameInput, ageInput, addButton, deleteButton);
VBox vBox = new VBox();
vBox.getChildren().addAll(table);
BorderPane borderpane = new BorderPane();
borderpane.setTop(gridpane);
borderpane.setCenter(vBox);
Scene scene = new Scene(borderpane);
window.setScene(scene);
window.show();
}
public void addButtonClicked() {
try {
User user = new User();
user.setFirstName(firstNameInput.getText());
user.setLastName(lastNameInput.getText());
user.setAge(Integer.parseInt(ageInput.getText()));
table.getItems().add(user);
firstNameInput.clear();
lastNameInput.clear();
ageInput.clear();
} catch (NumberFormatException empty) {
}
}
public void deleteButtonClicked() {
ObservableList<User> userSelected, allUsers;
allUsers = table.getItems();
userSelected = table.getSelectionModel().getSelectedItems();
try {
userSelected.forEach(allUsers::remove);
} catch (Exception e) {
}
}
public ObservableList<User> getUser() {
ObservableList<User> users = FXCollections.observableArrayList();
return users;
}
public void closeProgram() {
boolean answer = ConfirmBox.display("Confirmation", "Are you sure that you wish to exit?");
if (answer)
window.close();
}
}
用户类:
package test;
import java.io.Serializable;
public class User implements Serializable {
private String firstName;
private String lastName;
private int age;
public User() {
this.firstName = "";
this.lastName = "";
this.age = 0;
}
public User(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!