.txt in combo box
I am new in the world of programming and am completing a Java programming assignment. In my assignment I am having a problem trying to display a list of .txt files in a combo box. Can some body help me please? Here is the code that I am trying to use.
DefaultListModel listModel = new DefaultListModel();
Object list1Selected = list1.getSelectedItem();
String selected = list1Selected.toString();
System.out.println(selected);
String files;
File folder = new File("/Assignment_Datex/new_destination/msgsend0.txt",
selected);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
listModel.addElement(files);
开发者_C百科 }
list.setModel(null);
}
Why do you call list.setModel(null);
into the loop? Remove this line and call list.setModel(listModel);
after the loop.
精彩评论