1

I'm currently learning GUI applications from a book called "starting out with java". I've tried one of the author's code examples regarding JList, but it turns out that the getSelectedValues() in ButtonListener is already deprecated. I just want to ask if you guys know any alternatives for that specific code for that. Although the code still works tho but I still wanted to know alternatives.

Here is the code:

package Practice; 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Practice1 extends JFrame{
private JPanel monthPanel;
private JPanel selectedMonthPanel;
private JPanel buttonPanel;
private JList monthList;
private JList selectedMonthList;
private JScrollPane scrollPane1;
private JScrollPane scrollPane2;
private JButton button;

private String[]months = {"January","February","March","April",
        "May","June","July","August","September","October",
        "November","December"};

public Practice1(){
    setTitle("List Demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    buildMonthPanel();
    buildSelectedMonthPanel();
    buildButtonPanel();
    add(monthPanel,BorderLayout.NORTH);
    add(selectedMonthPanel,BorderLayout.CENTER);
    add(buttonPanel,BorderLayout.SOUTH);
    pack();
    setVisible(true);
}

private void buildMonthPanel(){
    monthPanel = new JPanel();
    monthList = new JList(months);
    monthList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    monthList.setVisibleRowCount(6);
    scrollPane1 = new JScrollPane(monthList);
    monthPanel.add(scrollPane1);
}

private void buildSelectedMonthPanel(){
    selectedMonthPanel = new JPanel();
    selectedMonthList = new JList();
    selectedMonthList.setVisibleRowCount(6);
    scrollPane2 = new JScrollPane(selectedMonthList);
    selectedMonthPanel.add(scrollPane2);
}

private void buildButtonPanel(){
    buttonPanel = new JPanel();
    button = new JButton("Get Selections");
    button.addActionListener(new ButtonListener());
    buttonPanel.add(button);
}

private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Object[]selections = monthList.getSelectedValues();
        selectedMonthList.setListData(selections);
    }
}

public static void main(String[]args){
    new Practice1();
}
}
Pearl
  • 123
  • 1
  • 7
  • 1
    From the [JavaDocs](http://docs.oracle.com/javase/8/docs/api/javax/swing/JList.html#getSelectedValues--) - *"As of JDK 1.7, replaced by getSelectedValuesList()"* – MadProgrammer Sep 24 '15 at 03:17
  • (1-) Did you try to read the API for that method? When a method is deprecated the API will tell you what method to use. You need to learn how to use the API to your advantage. – camickr Sep 24 '15 at 03:19

2 Answers2

0

Replace it with the following

monthList.getSelectedValuesList().toArray()

See This post for more information

Community
  • 1
  • 1
jthort
  • 409
  • 4
  • 14
  • 3
    Since you've really not add much to the original post (and I'd question the need to turn back to an array, but that's me), you really should have either made it comment or marked the question as a duplicate. As it stands, you're just riding the hard work of other people – MadProgrammer Sep 24 '15 at 03:26
  • @MadProgrammer I did leave a link for more information so it didn't seem like I was taking someone else's answer from the other post. But you're right, should have been a comment – jthort Sep 24 '15 at 16:34
0

In the docs, it says that getSelectedValues is deprecated and it says that this method is replaced by getSelectedValuesList! If you looked at the docs, you would have known that. Next time you encounter these kind of problem, just look at the docs. Here is the docs:

http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#getSelectedValuesList()

Note that the new method returns a List<E>, not a Object[]. You can just use the toArray method to convert it to Object[].

Sweeper
  • 213,210
  • 22
  • 193
  • 313