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();
}
}