Generics & Swing
December 5, 2007 · Programming
Generics & swing
Recently i wrote a debugging frame for my main Ph.D. project, a Java compiler with a extra goodies (will come to that as soon as i have a release, be patient :) ). I was surprised when i saw that the javax.swing.JList class has a constructor that takes as input a generic java.util.Vector.JList(Vector<?> listData) Constructs a JList that displays the elements in the specified VectorI originally thought that the usage of generics are so well integrated in the java development kit, that they took the time to modify the swing components. But i was wrong. To get the value of a selected item you must use the
Object getSelectedValue() Returns the value for the smallest selected cell index; the selected value when only a single item is selected in the list. Object[] getSelectedValues() Returns an array of all the selected values, in increasing order based on their indices in the list.As you can see, the result is a java.lang.Object. So, if you want to program some serious (or typical) swing application, you can forget about type-safety.