gpt4 book ai didi

java - 将文件中的值设置为组合框

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:02 24 4
gpt4 key购买 nike

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();

}

我正在尝试我的组合框 ChooseExisting 从提交的名称中获取值并“记住”它,以及如何做到这一点,以便每次我打开应用程序时,它都会从我已经创建的客户中获得下拉列表。

最佳答案

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){

}
}

这根本不是一个好的做法,我建议您为此使用数据库

关于java - 将文件中的值设置为组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43267547/

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