繁体   English   中英

尝试将项目从 txt 文件添加到 jList

[英]Attempting to add items to jList from txt File

我有以下在单击按钮时执行的 try 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);
  }

我可以成功System.out.println(line)所以我知道有些东西工作正常。 我无法使用文本文件中的行填充列表。 上面的代码告诉我cannot add containers parent to self.

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

存在许多错误,太多了,无法对所有错误发表评论:

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);

你真的不能这样做:再次阅读这一行: jList1.add(line, jList1); 你到底是什么意思? 您正在将 jList1 添加到 jList1,对吗? 检查代码并相应地修复它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM