简体   繁体   English

逐行从.txt阅读中填充JList

[英]Populate a JList from a .txt reading line by line

I want to populate a JList from a .txt I can't populate the JList... Here's the code: 我想从.txt填充一个JList,但我不能填充JList ...这是代码:

The .txt is formatted like this sample: .txt的格式类似于以下示例:

name1
name2
name3

The JList is declared in this way: JList以这种方式声明:

private javax.swing.JList jList1

This is the method used to read line by line: 这是用于逐行读取的方法:

 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) {
    }
}

Thanks 谢谢

Try 尝试

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

jList1.setModel(listModel);

and if i want to populate a Jtextarea instead of the jlist ???? 如果我想填充一个Jtextarea而不是jlist ????

Then use the read() method that is part of the API. 然后使用API​​的read()方法。 THere is no need to write custom code: 无需编写自定义代码:

textArea.read(...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM