Rather than change JLists, keep the same JList but simply change models. If you populate a DefaultListModel with your new Strings and call setModel(...)
on the JList, you should be good to go.
e.g.,
DefaultListModel newListModel = new DefaultListModel();
for (String text : newStringArray) {
newListModel.addElement(text);
}
originalJList.setModel(newListModel);
Edit
You state:
I did this but I'm not getting anything still. Check my updated question
and:
Okay, so my problem was that I wasn't setting a model. However, I'm still not getting any results after setting a model. Heres the code im using:
ScratchItem[] items = listPlaylists(line2);
DefaultListModel newListModel = new DefaultListModel();
for(ScratchItem item : items) {
newListModel.addElement(item);
}
JList list = new JList();
list.setModel(newListModel);
list.setVisible(true);
add(list, BorderLayout.SOUTH);
invalidate();
You're still creating a new JList. Don't do this but instead use the original JList as we've recommended.
ScratchItem[] items = listPlaylists(line2);
DefaultListModel newListModel = new DefaultListModel();
for(ScratchItem item : items) {
newListModel.addElement(item);
}
// JList list = new JList(); // *** don't create a new JList
// originalList refers to the original displayed JList
originalList.setModel(newListModel);
// list.setVisible(true);
// add(list, BorderLayout.SOUTH);
// invalidate();
Edit 2
You state in comment:
There IS no original JList. This is the first one I'm creating in the program. Should I just add one in the constructor, then here just change the model and setVisible(true)?
Then things are getting more complex. Your updated code in your question should work if everything else is correct, suggesting that everything else isn't correct. What is wrong is hard to say base on the code snippets you've posted so far. My suggestions:
- First and foremost, if you need to swap views in your Swing GUI, consider using a CardLayout as this makes the swapping much easier.
- Secondly, if you're tied to your current structure, then you'll need to show more code, preferably by posting a minimal code example that demonstrates your problem, an SSCCE. This will allow us to run your code and modify it and perhaps even correct it. Please read the link before replying as it supplies many important details on the SSCCE requirements.