Take the 2-minute tour ×

I am following a series of Java tutorials in an attempt to learn it. I have a question about tutorial 72.

Link: http://www.youtube.com/watch?v=9z_8yEv7nIc&feature=relmfu

At 7:02 of the video, this statement is written. However, this method has been deprecated in Java 1.7. (Informed by Eclipse).


Eclipse returns the following error:

Object[] javax.swing.JList.getSelectedValues()

public Object[] getSelectedValues()
Deprecated. As of JDK 1.7, replaced by getSelectedValuesList()
Returns an array of all the selected values, in increasing order based on their indices in the list.
the selected values, or an empty array if nothing is selected
See Also:
isSelectedIndex(int), getModel(), addListSelectionListener(javax.swing.event.ListSelectionListener)

But this returns an error saying 'The method setListData(Object[]) in the type JList is not applicable for the arguments (List)'.

What is the correct way to replace the above statement?

Also, I want to take this opportunity to ask a another unrelated question. Is it better to initialize variables outside the method like so:

    private         JList       LeftList    =   new JList();
    private         JList       RightList   =   new JList();
    private         JButton     Move        =   new JButton("Move -->");

    private static  String[]    Items       =   {"Item 1", "Item 2","Item 3","Item 4","Item 5"};

Compared to (As shown in the video): Declaring variables outside the class like above, but assigning values to them inside the method?

Does either perform better?

share|improve this question
You're asking about initializing variables, not declaring them. Also, that's not "outside the class", but rather outside a method. Where to initialize variables depends on how they're used and is often a matter of preference. The performance difference will usually be negligible, unless you're creating huge numbers of them and initializing variables on creation rather than as needed. –  Dave Newton Sep 18 '11 at 14:08
Alright, thanks for the reply. I tend to use terms like that interchangeably, sorry for any confusion. I should use the correct terms though, thanks for the reminder. =) –  Deley Sep 18 '11 at 14:09
What kind of variable is LeftList? Is it a JList? Also show the actual error message, not your interpretation of it. –  Hovercraft Full Of Eels Sep 18 '11 at 14:11
Dang -- you're using Java 1.7! I guess that I'd better get with the times! –  Hovercraft Full Of Eels Sep 18 '11 at 14:22
Yes, it's a JList. I have updated the question to show the error message. –  Deley Sep 18 '11 at 14:25
add comment

3 Answers

up vote 5 down vote accepted

According to JList javadoc for Java7 I see that indeed you have no option - the two APIs (getSelectedValuesList and setDataList) are unrelated.

To solve it, a simple solution would be to perform LeftList.getSelectedValuesList().toArray() - it will provide you with an array suitable for setDataList. Disclaimer: I don't know if this is the "correct" usage recommended by Java, but it should work.

Also, note that a deprecated API does not mean it doesn't work - if you feel you don't want to invest time in it now, you can still use the old API (like in your situation where you are doing a tutorial and not some ongoing product that will be in production for the next 10 years)

As for the 2nd question - it is a matter of taste, I prefer declaring the variables without initializing them in the class declaration and setting them with values in the constructor. It is customary to give initial values to constants (e.g. public static final String AAA = "XYZ"; )

share|improve this answer
That worked well, aside from a type error which I'm not concerned about. Again, I will try to use proper styling conventions. Thank you everyone. I wish I could accept all your answers, but I cannot. =) –  Deley Sep 18 '11 at 14:26
Great solution.I stumbled upon the same exact problem. –  skiabox Jan 3 '13 at 14:54
add comment

You'd need to update the setListData method to take the new parameter type (and any other code that was expecting an Object[], including methods, possible things that iterate over the array, etc.) Just because something is deprecated doesn't mean it's removed, though.

What to do depends on your immediate goal: is it to learn the material, or to learn the material and update all the source to compile without warnings.

share|improve this answer
Well, I was looking through the intelli-sense menu, but couldn't find anything else turning an object array. Should I just create a method to do the same thing? and yes, it would be beneficial to compile without warnings. However, I do not understand them, e.g. (JList is a raw type. References to generic type JList<E> should be parameterized). Thanks. –  Deley Sep 18 '11 at 14:16
No, you need to change your methods (i.e. the tutorial code) to use the return value types of the methods replacing the deprecated methods. Again, it depends on your goals: IMO trying to re-write the code and learn the material and toss generics in to the mix is a rough row to hoe. Pick something, focus on it first, then do the rest. –  Dave Newton Sep 18 '11 at 14:23
Alright, perhaps I am trying to make unnecessary changes. I'll keep that advice in mind. Thanks for your input. –  Deley Sep 18 '11 at 14:37
add comment

I looked at the tutorial in question.

For your question about API, you would need to do the following:


PS: some tips about style. In Java, variables usually begin with a lowercase alphabet and class names begin with an uppercase alphabet. In your code above, it looked to me that you were trying to call a static method on a class, so you might want to change the names to lowercase.

share|improve this answer
Ah, thank you. It works now, only giving me an error about types, which I'm happy to ignore. I'll keep the styling tip in mind, since it's probably more important to follow proper conventions than my own. =) –  Deley Sep 18 '11 at 14:24
add comment

Your Answer


