Java - obsolete method - what to do?

I am following a series of Java tutorials trying to learn them. I have a question about textbook 72.

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

At 7:02 the video this expression is written. However, this method is 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: "The setListData(Object[]) in the type JList is not applicable for the arguments (List)' method setListData(Object[]) in the type JList is not applicable for the arguments (List)' .

What is the correct way to replace the above statement?


In addition, I want to take this opportunity to ask another unrelated question. Is it better to initialize variables outside the method as follows:

  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 as described above, but assigning values ​​to them inside the method?

Is it doing better?

+4
source share
3 answers

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

To solve this problem, a simple solution would be to do LeftList.getSelectedValuesList().toArray() - it will provide you with an array suitable for setDataList . Disclaimer: I do not know if this is the “correct” use recommended by Java, but it should work.

Also note that the outdated API does not mean that it does not work - if you feel that you do not want to invest time in it, you can still use the old API (for example, in your situation when you are doing a tutorial, not some then a permanent product that will be in production over the next 10 years)

As for the second question - this is a matter of taste, I prefer to declare variables without initializing them in the class declaration and set them with values ​​in the constructor. Typically, the initial values ​​are given by constants (for example, public static final String AAA = "XYZ"; )

+9
source

You will need to update the setListData method to accept a new type of parameter (and any other code that expects Object[] , including methods, possible things that repeat through the array, etc.) Just because something is outdated, but not means it is being deleted.

What to do depends on your immediate goal: whether to study this material or study the material and update all source code for compilation without warning.

+3
source

I looked at the textbook in question.

For your API question you will need to do the following:

 rightList.setListData(leftList.getSelectedValuesList().toArray()); 

PS: some style tips. In Java, variables usually begin with a lowercase alphabet, and class names begin with an uppercase alphabet. In the above code, it seemed to me that you are trying to call a static method for a class, so you can change the names to lowercase.

+2
source

All Articles