this is the picture of the form I am working on 所以我有一个程序从用户输入(客户信息和地址)创建一个文件,我需要在创建一些记录后从文件中获取信息,只在组合中输入一个名称框并让该框从现有名称列表中进行选择以编辑客户。
到目前为止我的一些代码: 这是我从输入中写入文件:
private void createCustomer() {
//getting text from the fields
int id = Integer.parseInt(txfId.getText());
String name = String.valueOf(txfName.getText());
String lastName = String.valueOf(txfSurname.getText());
String buisness = String.valueOf(txfBuisness.getText());
String street = String.valueOf(txfStreet.getText());
String unit = String.valueOf(txfUnit.getText());
String city = String.valueOf(txfCity.getText());
String province = String.valueOf(txfProvince.getText());
String postal = String.valueOf(txfPostal.getText());
String email = String.valueOf(txfEmail.getText());
String phoneNumber = String.valueOf(txfPhone.getText());
Address address = new Address(unit, street, city, province, postal);
Customer customers = new Customer(id, name, lastName,
phoneNumber, email, address, buisness);
File file = new File("customer.txt");
//appending to a file
try (PrintWriter writer = new PrintWriter(new FileWriter(file, true))) {
writer.print("id: " + id);
writer.print(", First name: " + name);
writer.print(", Last name: " + lastName);
writer.print(", Phone Number: " + phoneNumber);
writer.print(", Email: " + email);
writer.print(address);
writer.println(", Buisness: " + buisness);
} catch (IOException ex) {
System.out.println(ex.toString());
}
chooseExisting.getItems().add(name);
//hooseExisting.setId(name);
chooseExisting.setValue(name);
chooseExisting.setOnAction(e -> {
});
//clearing the text after it saved
txfId.clear();
txfName.clear();
txfSurname.clear();
txfBuisness.clear();
txfStreet.clear();
txfUnit.clear();
txfCity.clear();
txfProvince.clear();
txfPostal.clear();
txfEmail.clear();
txfPhone.clear();
}
我正在尝试使用我的组合框选择现有来从名称中获取值并且"记住"它,以及我如何能够这样做,所以每次打开一个应用程序时,它都会从我创建的客户那里下载。
答案 0 :(得分:0)
对于应用来说,记住它"您需要在本地或数据库中存储数据。
如果你正在使用一个文件,你必须解析里面的字符串,我会使用for循环。要实际获取每个字符串,您必须使用文件阅读器。 Java有扫描仪。查找Scanner api。如果您发布了文本文件的图片,我可以帮助您解析它。
答案 1 :(得分:0)
在 initComponents()
之后,将 retriveNamaData()方法调用到构造函数中public Constructor() {
initComponents();
retriveNameData();
}
private void createCustomer() {
//getting text from the fields
int id = Integer.parseInt(txfId.getText());
String name = String.valueOf(txfName.getText());
String lastName = String.valueOf(txfSurname.getText());
String buisness = String.valueOf(txfBuisness.getText());
String street = String.valueOf(txfStreet.getText());
String unit = String.valueOf(txfUnit.getText());
String city = String.valueOf(txfCity.getText());
String province = String.valueOf(txfProvince.getText());
String postal = String.valueOf(txfPostal.getText());
String email = String.valueOf(txfEmail.getText());
String phoneNumber = String.valueOf(txfPhone.getText());
Address address = new Address(unit, street, city, province, postal);
Customer customers = new Customer(id, name, lastName,
phoneNumber, email, address, buisness);
File file = new File("customer.txt");
//appending to a file
try (PrintWriter writer = new PrintWriter(new FileWriter(file, true))) {
writer.print("id: " + id);
writer.print(" , First name: " + name);
writer.print(" , Last name: " + lastName);
writer.print(" , Phone Number: " + phoneNumber);
writer.print(" , Email: " + email);
writer.print(address);
writer.println(" , Buisness: " + buisness);
} catch (IOException ex) {
System.out.println(ex.toString());
}
retriveNameData();
//chooseExisting.getItems().add(name);
//hooseExisting.setId(name);
//chooseExisting.setValue(name);
//chooseExisting.setOnAction(e -> {
// });
//clearing the text after it saved
txfId.setText("");
txfName.setText("");
txfSurname.setText("");
txfBuisness.setText("");
txfStreet.setText("");
txfUnit.setText("");
txfCity.setText("");
txfProvince.setText("");
txfPostal.setText("");
txfEmail.setText("");
txfPhone.setText("");
}
// this method will retrieve the Name data from the text file.
private void retriveNameData(){
String line;
try{
InputStream fis = new FileInputStream("customer.txt");
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
String[] words = line.split(" ");
String item=words[5]+" "+words[9];
chooseExisting.addItem(item);
}
}catch(Exception e){
}
}
这根本不是一个好习惯,我建议您使用数据库。