Question

If i want to add an ArrayList to a JList, it is not shown in the list, but the array contains the items. How could i fix this?

  public void updateLeftList(){
        // Enter the search text
        interFace.Search.setText(this.search);

        // Get the left list
        JList leftList = interFace.LeftList;
        leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        // Define the arraylists
        allPdf = new ArrayList();
        curPdf = new ArrayList();
        addPdf = new ArrayList();

        // Get Files from manuals folder        
        File files = new File(configFile.getProperty("dir") + "/" + this.taal);
        File[] listFiles = files.listFiles();

        for(int i = 0; i < listFiles.length; i++){
            if(listFiles[i].getName().endsWith(".pdf") && listFiles[i].isFile()){
                allPdf.add(listFiles[i].getName().toString());
            }
        }
        leftList.setListData(allPdf.toArray());
    }
Was it helpful?

Solution

use ListModel to add the data:
1. create new instance of DefaultListModel
2. add your file names to the list model
3. set it as your JList list model

OTHER TIPS

for(Object item : arrayList.toArray()){
    ((DefaultListModel) list1.getModel()).addElement(item);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top