我想从.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);

10-08 09:19