Advertisement
thenewboston

Java Programming Tutorial - 72, 73 - Multiple Selection List

Aug 22nd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.JFrame;
  5. import javax.swing.JButton;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.JList;
  8. import javax.swing.ListSelectionModel;
  9.  
  10. class MultiGUI extends JFrame {
  11.    private JList leftList;
  12.    private JList rightList;
  13.    private JButton moveButton;
  14.    private String foods[ ] = {"fish", "beacon", "chicken", "ham", "egg", "milk", "apple"};
  15.    
  16.    public static void main(String args[ ]) {
  17.       MultiGUI gui = new MultiGUI();
  18.       gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.       gui.setSize(350, 150);
  20.       gui.setVisible(true);
  21.    }
  22.    
  23.    MultiGUI() {
  24.       super("super duper mover");
  25.       setLayout(new FlowLayout());
  26.      
  27.       leftList = new JList(foods);
  28.       leftList.setVisibleRowCount(5);
  29.       leftList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  30.       add(new JScrollPane(leftList));
  31.      
  32.       moveButton = new JButton("move -->");
  33.       moveButton.addActionListener(
  34.          new ActionListener() {
  35.             public void actionPerformed(ActionEvent event) {
  36.                rightList.setListData(leftList.getSelectedValues());
  37.             }
  38.          }
  39.       );
  40.       add(moveButton);
  41.       rightList = new JList();
  42.       rightList.setVisibleRowCount(5);
  43.       rightList.setFixedCellHeight(15);
  44.       rightList.setFixedCellWidth(100);
  45.       add(new JScrollPane(rightList));
  46.    }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement