尝试将 txt 文件中的项目添加到 jList
我有以下在单击按钮时执行的 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)
所以我知道有些东西工作正常。我无法使用文本文件中的行填充列表。上面的代码告诉我,我无法将容器父级添加到自身。
尝试查找更多信息只会让我更加困惑。我遇到过一些地方说 jLists 比这更复杂?
I have the following try catch block that executes on a button click.
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. 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.
Attempting to find more information has only confused me more. I have come across some places that say jLists are more complciated than this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存在很多错误,太多了,无法一一评论:
1) 基本 I/O
2) 异常
3) <一href="http://download.oracle.com/javase/tutorial/uiswing/components/list.html" rel="nofollow">如何使用列表
4) 示例
There are many mistakes present, too many to comment on all of them:
1) Basic I/O
2) Exceptions
3) How to Use Lists
4) Examples
你确实做不到:
再次阅读这一行:
jList1.add(line, jList1);
您真正的意思是什么?您正在将 jList1 添加到 jList1,对吧?检查代码并进行相应修复。You really cannot do it:
Read again this line:
jList1.add(line, jList1);
What did you really mean? You are adding jList1 to jList1, right? Check the code and fix it accordingly.