英文:
Java/Swing Save input to a file from multiple JTextField
问题
private void ZoneTexte(String texte, JPanel pan) {
JLabel label = new JLabel();
label.setText(texte);
JTextField text = new JTextField(20);
pan.add(label);
pan.add(text);
}
private void SaveParam() {
String[] param = {"Nom Milieu", "ProbaRoche", "ProbaHerbe", "Qherbe", "DistCaractHerbe", "FacteurHerbe", "RayonLac", "ProfondeurMaxLac", "SigmaLac"};
JLabel label = new JLabel();
label.setText("Décider de vos propres paramètres de simulation:");
EditPanel.add(label);
List<JTextField> textFields = new ArrayList<>(); // Create a list to store text fields
for (String s : param) {
JPanel pan1 = new JPanel();
ZoneTexte(s + " :", pan1);
EditPanel.add(pan1);
JTextField textField = (JTextField) pan1.getComponent(1); // Get the text field component
textFields.add(textField); // Add the text field to the list
}
JButton save = new JButton();
save.setText("Enregistrer et lancer la simulation");
EditPanel.add(save);
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO: Retrieve data from text fields and save to CSV
for (JTextField textField : textFields) {
String data = textField.getText();
// TODO: Process the data and save to CSV file
}
// TODO: Redirect to the new frame for simulation
}
});
}
Note: This code includes comments with "TODO" placeholders where you should implement the missing functionality. Make sure to retrieve the data from the text fields, process it, and save it to a CSV file as desired. Additionally, the code doesn't include the actual redirection to the new frame for simulation, so you need to implement that part as well.
英文:
i'm trying to code a frame that get from the user a bunch of parameters and save them into a csv file and then redirect to a new frame that run a simulation, the problem is used a for loop to generate easily the text fields however i don't know how to write the event listener that retrive the data from all the text fields ,here's the code:
private void ZoneTexte(String texte,JPanel pan) {
JLabel label=new JLabel();
label.setText(texte);
JTextField text = new JTextField(20);
pan.add(label);
pan.add(text);
}
/**
*
*/
private void SaveParam() {
String [] param= {"Nom Milieu","ProbaRoche","ProbaHerbe","Qherbe","DistCaractHerbe","FacteurHerbe","RayonLac","ProfondeurMaxLac","SigmaLac"};
JLabel label=new JLabel();
label.setText("Définir vos propres paramètres de simulation:");
EditPanel.add(label);
for(String s:param) {
JPanel pan1= new JPanel();
ZoneTexte(s+" :",pan1);
EditPanel.add(pan1);
}
JButton save=new JButton();
save.setText("Enregistrer et démarrer la simulation");
EditPanel.add(save);
save.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
}
[The file format that i desire is this][1]
答案1
得分: 0
保留文本字段在一个列表中,并在保存时进行迭代。
List<JTextField> fieldList = new ArrayList<>();
...
EditPanel.add(pan1)
fieldList.add(pan1);
}
save.addActionListener(e -> {
for (JTextField field : fieldList) {
file.writeString(field.getText); // 以您希望的方式编写
}
}
英文:
Keep the text fields in a list and iterate it when saving.
List<JTextField> fieldList = new ArrayList <>() ;
...
EditPanel.add(pan1)
fieldList.add(pan1);
}
save.addActionListener(e -> {
for (JTextField field : fieldList) {
file.writeString(field.getText); // however you want to write it
}
}
答案2
得分: 0
IMHO,您需要使用类似于Map的结构来保持对每个TextField的引用。然后在您的监听器中,您将解析您的内存结构,以便填充您的CSV。
// 在您的初始化类中的某个位置
var fieldsMap = new HashMap<String,TextField>();
// 在ZoneTexte方法中
fieldsMap.put(texte,text);
// 在您的监听器中
var value = mapField.get("ProbaRoche").getText();
也许可以使用字段的唯一键,该键可以在代码的不同位置使用。
英文:
IMHO, you need to use a structure like a Map to keep a reference to each TextField. Then in your listener, you will parse your memory structure so that filling your csv.
// Somewhere in your init class
var fieldsMap = new HashMap<String,TextField>();
// In ZoneTexte method
fieldsMap.put(texte,text);
// In your listener
var value = mapField.get("ProbaRoche").getText();
Perhaps, use a key unique for the field that can be used in different location of your code.
HTH
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论