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.
RightList.setListData(LeftList.getSelectedValues());
Eclipse returns the following error:
Object[] javax.swing.JList.getSelectedValues()
getSelectedValues
@Deprecated
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.
Returns:
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?
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";
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With