如何将文本文件中的行复制到 JComboBox 中?
我试图将文本文件的每一行复制到 jcomboBox 中,但它仅在 jcomboBox 中显示文本文件的第一行...我不明白为什么。 你能解释一下出了什么问题吗?
(...)
BufferedReader in;
String read;
try {
in = new BufferedReader(new FileReader("D:/File.txt"));
read = in.readLine();
lines[w]=read;
++w;
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
combo1 = new JComboBox(lines);
combo1.setPreferredSize(new Dimension(100,20));
combo1.setForeground(Color.blue);
JPanel top = new JPanel();
top.add(label);
top.add(combo1);
combo1.addActionListener(new ActionFichiers());
container.add(top, BorderLayout.NORTH);
this.setContentPane(container);
this.setVisible(true);
}
(...)
I'm trying to copy each line of a text file into a jcomboBox, but it displays only the first line of the text file in the jcomboBox...I don't understand why.
Can you please explain me what's wrong?
(...)
BufferedReader in;
String read;
try {
in = new BufferedReader(new FileReader("D:/File.txt"));
read = in.readLine();
lines[w]=read;
++w;
in.close();
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
combo1 = new JComboBox(lines);
combo1.setPreferredSize(new Dimension(100,20));
combo1.setForeground(Color.blue);
JPanel top = new JPanel();
top.add(label);
top.add(combo1);
combo1.addActionListener(new ActionFichiers());
container.add(top, BorderLayout.NORTH);
this.setContentPane(container);
this.setVisible(true);
}
(...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您只读取了第一行并关闭了文件,请考虑:
注意:我假设数组
lines
足够大That's because you read only the first line and close the file, consider:
Note: I assume the array
lines
is big enough您只读取文件的第一行。所以 JCombobox 中不能有更多内容。您应该花一些时间阅读所有行,直到读完。
You're only reading the first line of the file. So there can't be more in the JCombobox. You should use a while and read all lines till you reach the end.
替换
为
replace
with