Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JButton上单击(Actionlistener),将来自jtextfields的数据放入arraylist_Java_Multithreading_Arraylist_Actionlistener_Jtextfield - Fatal编程技术网

Java 在JButton上单击(Actionlistener),将来自jtextfields的数据放入arraylist

Java 在JButton上单击(Actionlistener),将来自jtextfields的数据放入arraylist,java,multithreading,arraylist,actionlistener,jtextfield,Java,Multithreading,Arraylist,Actionlistener,Jtextfield,对不起,如果我对我的问题的解释有点笨拙的话 我尝试添加X个JTextFields,并将每个字段的内容(int)添加到arrayList中。我想在单击“提交”按钮时发送包含此信息的arraylist 这里是一个循环,它创建JTextFields,并将字段中的数据添加到arraylist中 If I enter antalVare = new JTextField("0"), the 0 will be added to the arraylist, 但单击我的JButton时,它应该用JTex

对不起,如果我对我的问题的解释有点笨拙的话

我尝试添加X个JTextFields,并将每个字段的内容(int)添加到arrayList中。我想在单击“提交”按钮时发送包含此信息的arraylist

这里是一个循环,它创建JTextFields,并将字段中的数据添加到arraylist中

If I enter antalVare = new JTextField("0"),
the 0 will be added to the arraylist, 
但单击我的JButton时,它应该用JTextFields中的数据再次填充arraylist。我该怎么做?我尝试了使用线程的不同方法,但失败了

    kundeOrdreArrayList = new ArrayList<String>();

    alleVarerList = kaldSQL.alleVarer(connectDB);

    try {
        while (alleVarerList.next()) {
            antalVare = new JTextField();

            innerPanel.add(new JLabel(alleVarerList.getString(2) + " ("
                    + alleVarerList.getString(3) + ",- kr.)"));
            innerPanel.add(antalVare);
            innerPanel.add(new JLabel(""));
            kundeOrdreArrayList.add(antalVare.getText());
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    innerPanel.add(new JLabel(""));
    innerPanel.add(submit);
    innerPanel.add(new JLabel(""));

在您的第一段代码中,您添加到
kundeordrearaylist
的值是此时文本字段的值。之后更改文本字段时,这些值不会更新

因此,在ActionListener中,您需要再次迭代所有JTextFields。要做到这一点,首先更改您的第一段代码,以跟踪您拥有的所有JTextFields。所以 在类中添加一个新字段“ArrayList textfields”,然后(用
/+/++

textfields = new ArrayList<JTextField>(); // ++

try {
    while (alleVarerList.next()) {
        antalVare = new JTextField();
        textfields.add(antalVare); // ++

        innerPanel.add(new JLabel(alleVarerList.getString(2) + " ("
                + alleVarerList.getString(3) + ",- kr.)"));
        innerPanel.add(antalVare);
        innerPanel.add(new JLabel(""));
        kundeOrdreArrayList.add(antalVare.getText());
    }

我不清楚这两段代码是如何关联的。我想,第二段代码是单击JButton后执行的代码?第二段代码在哪里,何时执行?根据您的问题,我想您需要从ActionListener调用这段代码。请更清楚地说明,这些代码是如何执行的K段代码是相关的。第一段代码是在类执行时运行的。是的,第二段代码是在单击“提交”按钮时运行的。我清除了ActionListener,因为它们都不起作用。问题中的错误代码不一定是坏事。它向人们展示了您尝试了什么,可能会帮助他们识别错误问题。在写我的答案时,我实际上查阅了上一个版本,看到您刚刚输出了在“类被执行”时计算的值。好的!非常感谢,它现在工作得很好:)
textfields = new ArrayList<JTextField>(); // ++

try {
    while (alleVarerList.next()) {
        antalVare = new JTextField();
        textfields.add(antalVare); // ++

        innerPanel.add(new JLabel(alleVarerList.getString(2) + " ("
                + alleVarerList.getString(3) + ",- kr.)"));
        innerPanel.add(antalVare);
        innerPanel.add(new JLabel(""));
        kundeOrdreArrayList.add(antalVare.getText());
    }
  if (a.getSource().equals(submit)) {
      kundeOrdreArrayList.clear();
      for (JTextField field : textfields) {
           kundeOrdreArrayList.add(field.getText());
      }
  }