简体   繁体   English

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

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

I have the following try catch block that executes on a button click.我有以下在单击按钮时执行的 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);
  }

I can successfully System.out.println(line) so I know something is working right.我可以成功System.out.println(line)所以我知道有些东西工作正常。 I am unable to populate the list with the lines from the text file.我无法使用文本文件中的行填充列表。 The above code tells me I cannot add containers parent to self.上面的代码告诉我cannot add containers parent to self.

Attempting to find more information has only confused me more.试图找到更多信息只会让我更加困惑。 I have come across some places that say jLists are more complciated than this?我遇到过一些地方说 jLists 比这更复杂?

There are many mistakes present, too many to comment on all of them:存在许多错误,太多了,无法对所有错误发表评论:

1) Basic I/O 1)基本 I/O

2)Exceptions 2)例外情况

3) How to Use Lists 3)如何使用列表

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

You really cannot do it: Read again this line: jList1.add(line, jList1);你真的不能这样做:再次阅读这一行: jList1.add(line, jList1); What did you really mean?你到底是什么意思? You are adding jList1 to jList1, right?您正在将 jList1 添加到 jList1,对吗? Check the code and fix it accordingly.检查代码并相应地修复它。

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

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