简体   繁体   中英

Java DefaultListModel not added to JList

I am attempting to follow the MVC pattern so have 3 classes. The elements are being added to the model successfully and then passed onto the view. Nothing is using shown in the JList which appears on the screen, it just remains blank. Heres the code and thanks to anyone that can help! When I set my albumLabel text to the size of model it gets set to 1050, so I can assume that the data is getting back to my view class, just not into the display. Everything is declared and I have left out some bits which are not related to this.

My Controller class

class BrowseListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {            
        chooser = new JFileChooser(); 
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);        
        theModel.getMusicList(chooser);

        theView.setListModel(theModel.getListModel());
        theView.updateUI();
    }
}

My Model Class

public ArrayList<File> getMusicList(JFileChooser chooser){
    if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){ 
        File folder = new File(chooser.getSelectedFile().getPath());
        displayDirectoryContents(folder);
        if(allMusic.size() <= 0)
        {
            System.out.println("No music files found");
        }
    }
    else{
        System.out.println("No Selection ");
    }
    return allMusic;
} 

public void displayDirectoryContents(File dir) {
    File[] files = dir.listFiles();

    for (File file : files) {
        if (file.isDirectory()) {
            displayDirectoryContents(file);
        }
        else if(file.getName().endsWith(".mp3")) {
            allMusic.add(file);
            model.addElement(file.getAbsolutePath());
            System.out.println("file:" + file.getAbsolutePath());
        }           
    }
}

public DefaultListModel getListModel(){
    return model;
}

My View Class

MP3View(){        

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 200);

    model = new DefaultListModel();
    musicJList = new JList(model);

    metaDataPanel.add(new JScrollPane(musicJList), BorderLayout.CENTER);

    this.add(metaDataPanel);        
}

public void setListModel(DefaultListModel model){
    this.model = model;
    albumLabel.setText(Integer.toString(model.getSize()));
}

void addBrowseMusicListener(ActionListener listenForBrowse){
    browseButton.addActionListener(listenForBrowse);
}

You are not setting the model to the JList .

When you do,

model = new DefaultListModel();
musicJList = new JList(model);

The JList 's model is set to the new DefaultListModel() object.

But after that, you are doing,

theView.setListModel(theModel.getListModel()); // You should put this new model to the JList.

So, in you setListModel() method, do

public void setListModel(DefaultListModel model){
    this.model = model;
    this.musicJList.setModel(this.model); // Add this line.
    albumLabel.setText(Integer.toString(model.getSize()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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