Java/Swing 从多个JTextField保存输入到文件

huangapple go评论78阅读模式
英文:

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= {&quot;Nom Milieu&quot;,&quot;ProbaRoche&quot;,&quot;ProbaHerbe&quot;,&quot;Qherbe&quot;,&quot;DistCaractHerbe&quot;,&quot;FacteurHerbe&quot;,&quot;RayonLac&quot;,&quot;ProfondeurMaxLac&quot;,&quot;SigmaLac&quot;};
JLabel label=new JLabel();
label.setText(&quot;D&#233;finir vos propres param&#232;tres de simulation:&quot;);
EditPanel.add(label);
for(String s:param) {
JPanel pan1= new JPanel();
ZoneTexte(s+&quot; :&quot;,pan1);
EditPanel.add(pan1);
}
JButton save=new JButton();
save.setText(&quot;Enregistrer et d&#233;marrer la simulation&quot;);
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&lt;JTextField&gt; fieldList = new ArrayList &lt;&gt;() ;
...
EditPanel.add(pan1)
fieldList.add(pan1);
}
save.addActionListener(e -&gt; {
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&lt;String,TextField&gt;();
// In ZoneTexte method
fieldsMap.put(texte,text);      
// In your listener
var value = mapField.get(&quot;ProbaRoche&quot;).getText();

Perhaps, use a key unique for the field that can be used in different location of your code.

HTH

huangapple
  • 本文由 发表于 2020年5月5日 19:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定