Fseee
Fseee

Reputation: 2631

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:

The .txt is formatted like this sample:

name1
name2
name3

The JList is declared in this way:

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

Upvotes: 1

Views: 7889

Answers (2)

camickr
camickr

Reputation: 324118

and if i want to populate a Jtextarea instead of the jlist ????

Then use the read() method that is part of the API. THere is no need to write custom code:

textArea.read(...);

Upvotes: 1

Bala R
Bala R

Reputation: 108957

Try

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

jList1.setModel(listModel);

Upvotes: 4

Related Questions