[英]How to save and load a listview in Javafx?
首先,我是编程新手,所以请放轻松,因为到目前为止我所做的一切都是我第一次这样做。
基本上我正在尝试创建一个自动登录程序,该程序只需将用户名和密码输入到我每天使用多个帐户登录的网站中,所以我不是总是输入它,而是我正在尝试制作一个可以为我做的程序当我选择所需的帐户并单击运行时。
我使用 selenium 和 javafx 来完成所有这些工作,直到现在我已经碰壁一个月了。
我可以创建一个帐户并将其添加到列表中,但现在我想要的是能够将我的列表视图保存在某处,并且当程序启动时能够再次加载它,以便我之前创建的所有帐户还在那里。
这就是我坚持的地方,我查看了各种网站如何保存它,但它们都不适合我,因为它们使用了额外的东西,比如 jQuery 或其他一些我不知道如何使用的东西,所以如果我可以在我遇到的这个问题上获得一些帮助,我将非常感激。
我不知道是否需要为我的问题发布代码,但我会发布我如何创建 Listview 以及如何添加用户。
如果您觉得可以帮助我但需要更多信息,请在评论中通知我。
public class Controller {
@FXML
Button btnAdd, btnDelete, btnRun;
@FXML
public ListView<String> userName;
@FXML
public ListView<String> listMail;
@FXML
public ListView<String> listPass;
@FXML
private TextField txtUser;
@FXML
private TextField txtMail;
@FXML
private PasswordField txtPass;
@FXML
void onButtonAdd(MouseEvent event) {
if (txtUser.getText().trim().isEmpty() || txtMail.getText().trim().isEmpty() || txtPass.getText().trim().isEmpty()) {
Alert fail= new Alert(AlertType.ERROR);
fail.setHeaderText("Error");
fail.setContentText("All fields must be entered");
fail.showAndWait();
}
else {
userName.getItems( ).add(txtUser.getText());
listMail.getItems().add(txtMail.getText());
listPass.getItems().add(txtPass.getText());
}
}
这个应用程序演示了如何将数据从ListView
保存到文本文件,以及如何将数据从文本文件加载到ListView
。 如另一个答案所述,还有其他存储数据的方法。 示例:数据库、CSV、TSV、Excel、JSON 等。如果您采用不同的方法,这个答案可能会变得更加复杂。 我喜欢并推荐 SQLite、JSON、TSV 或 CSV。
使用Button
保存ListView
数据
Button btnSave = new Button("Save");
btnSave.setOnAction((t) -> {
try
{
saveFile.createNewFile();
Files.write(saveFile.toPath(), listView.getItems().subList(0, listView.getItems().size()));
getHostServices().showDocument(saveFile.getAbsolutePath());//Used to open the file the ListView info is saved to.
}
catch (IOException ex)
{
ex.printStackTrace();
}
});
将文本文件数据加载到ListView
if(saveFile.exists())
{
try
{
List<String> linesLoadedFromFile = Files.readAllLines(saveFile.toPath());
listView.getItems().addAll(linesLoadedFromFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
完整代码
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application
{
public static void main(String[] args)
{
launch();
}
ListView<String> listView = new ListView();
File saveFile = new File("save_here.txt");
@Override
public void start(Stage stage)
{
//Set the ListView cell factory. Add a Button to allow rows to be deleted for testing purposes.
listView.setCellFactory((ListView<String> param) -> {
Button btnDelete = new Button("Delete!");
final ListCell<String> cell = new ListCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(null);
setGraphic(null);
if (item != null || !empty) {
setText(item);
setGraphic(btnDelete);
btnDelete.setOnAction((t) -> {
getListView().getItems().remove(getItem());
});
}
}
};
return cell;
});
//Used to add text to the ListView
TextField tfAdd = new TextField();
Button btnAdd = new Button("Add");
btnAdd.setOnAction((t) -> {
if(tfAdd.getText().length() > 0)
{
listView.getItems().add(tfAdd.getText());
}
});
//Used to save the current state of the ListView
Button btnSave = new Button("Save");
btnSave.setOnAction((t) -> {
try
{
saveFile.createNewFile();
Files.write(saveFile.toPath(), listView.getItems().subList(0, listView.getItems().size()));
getHostServices().showDocument(saveFile.getAbsolutePath());//Used to open the file the ListView info is saved to.
}
catch (IOException ex)
{
ex.printStackTrace();
}
});
//Load file data to ListView
if(saveFile.exists())
{
try
{
List<String> linesLoadedFromFile = Files.readAllLines(saveFile.toPath());
listView.getItems().addAll(linesLoadedFromFile);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
HBox addRoot = new HBox(tfAdd, btnAdd);
addRoot.setAlignment(Pos.CENTER);
VBox root = new VBox(listView, addRoot, btnSave);
root.setSpacing(3);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.show();
}
}
如果您只想保存数据,您可以将列表作为 json 对象保存在数据库或文件中。
如果您决定使用文本文件,您可以将列表序列化为 json,然后将您创建的 3 个 json 对象(每个列表一个)放在文件中的单独行上。 当您重新打开读取文件的程序时,读取行,然后将 json 对象转换回列表。
如果您决定使用数据库,您首先必须在您的电脑中设置一个数据库,然后创建一个包含您要保存的各种数据的表。
恕我直言,使用文件替代方案将花费您更少的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.