MULTISELECTION LIST WHERE YOU CAN SELECT MULTIPLE ITEMS FROM A LIST TO BE MOVED TO ANOTHER LIST WHEN MOVE BUTTON CLICKED
import javax.swing.*;
class First{
public static void main(String args[]){
Second gui = new Second();
gui.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
gui.setSize(300, 200);
gui.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Second extends JFrame {
private JList leftList, rightList;
private JButton moveButton;
private static String[] foods = {"ham", "bacon", "beef", "salad", "potatoes"};
public Second(){
super("Title");
setLayout(new FlowLayout());
leftList = new JList(foods);
leftList.setVisibleRowCount(4);
leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftList));
moveButton = new JButton("Move -->");
moveButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
rightList.setListData(leftList.getSelectedValues());
}
}
);
add(moveButton);
rightList = new JList();
rightList.setVisibleRowCount(4);
rightList.setFixedCellHeight(15);
rightList.setFixedCellWidth(100);
rightList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(rightList));
}
}