How to I make a GUI list out of an array of objects?

Ty_ picture Ty_ · Jun 2, 2013 · Viewed 7.2k times · Source

I have an array of Objects. These objects are simple, just two Strings. I have a toString() method that just returns one of those strings.

I need to take that array of objects and make it into a visible GUI list so that the user can pick one of them.

In main I take a long string and send it to a method that parses it and returns an array of my objects. Then I make a new JList, add it, and make it visible:

JList list = new JList(objects);
list.setVisible(true);
add(list);

However, nothing appears. I already have a GUI present on the screen at this point. In fact, the class that the above code is contained in extends JPanel. I'm not sure why I'm not seeing anything, so I figure that I'm doing something wrong.

EDIT**

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();

Answer

Hovercraft Full Of Eels picture Hovercraft Full Of Eels · Jun 2, 2013

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.