尝试从txt文件向jList添加项目

sea*_*alz 0 java swing netbeans jframe jlist

我有以下尝试catch块,单击按钮执行.

  try {
//picking up the file I want to read in
 BufferedReader in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
 String line;                                           
 try {
    //read through the file until there is nothing left and add each line to list
         while((line = in.readLine()) != null){  
            jList1.add(line, jList1);
                    }

               } catch (IOException ex) {
                    Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
           }
      } catch (FileNotFoundException ex) {
                Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
  }
Run Code Online (Sandbox Code Playgroud)

我能成功,System.out.println(line)所以我知道有些事情是正确的.我无法使用文本文件中的行填充列表.上面的代码告诉我我cannot add containers parent to self.

试图找到更多信息只会让我更加困惑.我遇到过一些地方,说jLists比这更复杂?

mKo*_*bel 5

存在许多错误,太多不能评论所有错误:

1)基本I/O.

2)例外情况

3)如何使用列表

4)例子

    BufferedReader in = null;
    String line;
    DefaultListModel listModel = new DefaultListModel();
    try {
        in = new BufferedReader(new FileReader("C:\\users\\me\\desktop\\blah.txt"));
        while ((line = in.readLine()) != null) {
            listModel.addElement(line); //(String.valueof(line));
        }
    } catch (IOException ex) {
        Logger.getLogger(Frame2.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (in != null) {
            in.close();
        }
    }
    JList jList1 = new JList(listModel);
Run Code Online (Sandbox Code Playgroud)