gpt4 book ai didi

java - 从 .txt 逐行读取填充 JList

转载 作者:行者123 更新时间:2023-11-29 08:15:27 25 4
gpt4 key购买 nike

我想从 .txt 填充 JList我无法填充 JList...这是代码:

.txt 的格式类似于此示例:

name1
name2
name3

JList 是这样声明的:

private javax.swing.JList jList1

这是逐行读取的方法:

 private void visualizzaRosa(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
fileSquadra = squadra.getText();
try {
FileInputStream fstream = new FileInputStream("C:/Users/Franky/Documents....");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
Jlist1.add(strline); //to populate jlist
System.out.println(strLine); //to print on consolle
}
in.close();
} catch (Exception e) {
}
}

谢谢

最佳答案

尝试

DefaultListModel listModel = new DefaultListModel();
while ((strLine = br.readLine()) != null)
{
listModel.addElement(strline);
System.out.println(strLine);
}

jList1.setModel(listModel);

关于java - 从 .txt 逐行读取填充 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5175211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com