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(s开发者_如何学编程trline); //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 ????
Then use the read() method that is part of the API. THere is no need to write custom code:
textArea.read(...);
精彩评论