Java 试图将项目从txt文件添加到jList
我有下面的try-catch块,它在单击按钮时执行Java 试图将项目从txt文件添加到jList,java,swing,netbeans,jframe,jlist,Java,Swing,Netbeans,Jframe,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
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)
,所以我知道有些东西工作正常。我无法用文本文件中的行填充列表。上面的代码告诉我我无法将容器父容器添加到self。
试图找到更多的信息只会让我更加困惑。我遇到过一些地方说jLists比这更复杂?你真的做不到:
再次阅读此行:
jList1.add(行,jList1)你到底是什么意思?您正在将jList1添加到jList1,对吗?检查代码并进行相应的修复。存在许多错误,太多,无法对所有错误进行评论:
(一)
(二)
(三)
(四)
作为您的个人调试程序:-)FileReade
必须是FileReader
阅读教程。这是基本的东西。可能是重复的
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);