如何使用按钮将ArrayList中的值设置为JTextField?

时间:2016-12-01 13:42:25

标签: java jtextfield jlist

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter =new FileNameExtensionFilter ("Text/Java files","txt","java");
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
BufferedReader in = new BufferedReader(new FileReader("filename"));
String str;

ArrayList<String> list = new ArrayList<>();
while ((str = in.readLine()) != null) {
    list.add(str);
}
String[] listArray =list.toArray(new String[list.size()]);
for (int b=0; b<listArray.length;b++) {
    String[] Arra= str.split(" ");

    jTextfield1.setText(Arra.get(0));
    jTextfield2.setText(Arra.get(0));

1 个答案:

答案 0 :(得分:2)

首先,将bufferedReader更改为:

BufferedReader in = new BufferedReader(new FileReader(filename));//remove quotes around filename

您试图从名为FileReader的文件创建filename,而不是从文件名本身创建<{p}}

Arra是一个数组,但您将其用作列表。

请改为:

if(Arra.length > 1){
    jTextfield1.setText(Arra[0]);
    jTextfield2.setText(Arra[1]); 
}

if语句很重要,因为Arra可能没有索引0或1