Advertisement
makispaiktis

11. JList - Multiple selection - 2 lists

May 30th, 2022 (edited)
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class Gui extends JFrame{
  7.  
  8.     // Variables
  9.     private JList leftlist;
  10.     private JList rightlist;
  11.     private JButton movebutton;
  12.     private static String[] foods = {"bacon", "wings", "ham", "beef", "more bacon"};
  13.  
  14.  
  15.     // Constructor
  16.     public Gui(){
  17.  
  18.         super("JFrame's title");
  19.         setLayout(new FlowLayout());
  20.         leftlist = new JList(foods);
  21.         leftlist.setVisibleRowCount(3);     // 3 items seen by default
  22.         leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  23.         add(new JScrollPane(leftlist));
  24.         // add(leftlist);
  25.  
  26.         movebutton = new JButton("Move --->");
  27.         movebutton.addActionListener(
  28.                 new ActionListener() {
  29.                     @Override
  30.                     public void actionPerformed(ActionEvent event) {
  31.                         rightlist.setListData(leftlist.getSelectedValues());
  32.                     }
  33.                 }
  34.         );
  35.         add(movebutton);
  36.  
  37.         rightlist = new JList();
  38.         rightlist.setVisibleRowCount(3);
  39.         rightlist.setFixedCellHeight(15);
  40.         rightlist.setFixedCellWidth(100);
  41.         rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  42.         add(new JScrollPane(rightlist));
  43.  
  44.     }
  45.  
  46.     // Main Function
  47.     public static void main(String[] args){
  48.         Gui gui = new Gui();
  49.         gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
  50.         gui.setSize(600, 600);
  51.         gui.setVisible(true);
  52.     }
  53.  
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement