Russel Winder's Website

Constructing `JList`s

I have just been using a JList for the first time in ages. I see that (in JDK1.5 -- why isn't it JDK5.0?) there is a constructor taking an array and one taking a Vector but not one taking a List, ArrayList or LinkedList. I would have thought that the requirement for the data to a JList was that it was a sequence. In Java, List is the type for a sequence so why isn't it allowed? Why pick out Vector for special treatment? I can see that discrimination against LinkedList is more or less acceptable because a LinkedList is not a random access sequence -- indexed access is O(n) instead of O(1) and so this is not a good data structure for the model of a JList. ArrayList however is a perfectly adequate random access sequence yet you have to use ArrayList.toArray() to create an array to provide the model for a JList.

Unless I am missing something really obvious, it seems that the people who develop Swing do not believe in The Collections Framework. Sun say that ArrayList is to be preferred over Vector but it seems that Sun is not listening to its own message, continuing to ignore Collections in Swing. I assume that the use of Vector is a hang over from pre-Collections days, which is many, many moons ago now. Given that a Vector is a List and the toArray method works fine, why haven't they deprecated the Vector constructor and/or replaced it with a List constructor?

If anyone knows the answer to this trivial but irritating conundrum do tell.

Copyright © 2017 Russel Winder -