1

i am trying to add ArrayList to Jlist, but the only way that I given to understand is that to write the code like this :

ArrayList<String> labels = new ArrayList<String>();
JList jlist = new JList(labels.toArray());

Whats confusing me is that, why i cannot just add ArrayList to Jlist directly like this :

ArrayList<String> labels = new ArrayList<String>();
JList jlist = new JList(labels);

thanks in advance.

3
  • "Whats confusing me is that, why i cannot just add ArrayList to Jlist directly like this:" --- Because we can't just make up code and hope that it might work. The API will tell you exactly what constructors and methods are available. The more you use it the more useful it will become. Commented Jun 21, 2013 at 1:45
  • 1
    If you're really upset about it, create a ListModel that uses the List as it's core backing data... Commented Jun 21, 2013 at 1:51
  • See also: stackoverflow.com/a/3269560/363573 Commented Mar 7, 2015 at 15:27

4 Answers 4

7

The inclusion of "helper" constructors was meant to make it easier to use the JList with simple data structures.

The JList (and many Swing components) are actually meant to be used with models that supply the actual data to the view.

The original design goes way back before Swing was incorporated into the main library (prior to JDK 1.3) and just before the collections API was introduced, so it's likely that the original developers didn't have List available to them (hence the inclusion of Vector).

It's likely that no one has seen fit to update the libraries since (partially because it may have been decided that the original constructors shouldn't have been included, but I wasn't at that meeting ;))

A better/simpler solution would be to create your own model that uses the List as it's data source.

For example...

public class MyListModel<T> extends AbstractListModel<T> {

    private List<T> people;

    public MyListModel(List<T> people) {
        this.people = people;
    }

    @Override
    public int getSize() {
        return people.size();
    }

    @Override
    public T getElementAt(int index) {
        return people.get(index);
    }
}

Then you can simply supply it to the JList when ever you need to...

JList myList = new JList(new MyListModel<MyObject>(listOfMyObjets));
Sign up to request clarification or add additional context in comments.

1 Comment

While both Collections and Swing debuted in Java 1.2 (in 2000), Swing was available earlier as a stand-alone library.
3

JList has no constructor that accepts a List. The first example works as it uses the constructor JList(Object[]).

Familiarize yourself with the javadoc

Comments

2

Because JList doesn't have a constructor that accepts an ArrayList (or a List). It can only accept an array, a ListModel, or a Vector. See the documentation.

An ArrayList is not an array. You can't pass an ArrayList to a method that wants an array.

Comments

2

It's simple. JList constructors don't expect ArrayList

JList()
Constructs a JList with an empty, read-only, model.
JList(E[] listData)
Constructs a JList that displays the elements in the specified array.
JList(ListModel<E> dataModel)
Constructs a JList that displays elements from the specified, non-null, model.
JList(Vector<? extends E> listData)
Constructs a JList that displays the elements in the specified Vector.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.