How to Transfer selected objects from one JList to another JList in swing?

后端 未结 4 526
感动是毒
感动是毒 2021-01-28 03:50

I want to transfer the selected objects from one JList to another JList, say List1 and List2.

4条回答
  •  -上瘾入骨i
    2021-01-28 04:40

    Here I am showing you example of transferring name of cities from list to list_1. You will receive output something like this:

    Code for this is :

    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.DefaultListModel;
    
    import java.awt.Font;
    import javax.swing.JScrollPane;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class ListTransfer {
    
    private JFrame frame;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ListTransfer window = new ListTransfer();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the application.
     */
    public ListTransfer() {
        initialize();
    }
    
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
    
        String[] values = new String[] {"Surat", "Ahmedabad", "Vadodra", "Anand", "Bharuch", "Maninagar", "Mumbai", "Pune", "Hydrabad", "Banglore"};
    
        DefaultListModel model_list = new DefaultListModel();
        DefaultListModel model_list_1 = new DefaultListModel();
        for (String value : values) {
            model_list.addElement(value);
        }
    
        JList list = new JList();
        list.setFont(new Font("Lucida Grande", Font.PLAIN, 21));
        list.setBounds(17, 41, 174, 194);
        list.setModel(model_list);
    
        JScrollPane listScroller = new JScrollPane();
        listScroller.setLocation(15, 43);
        listScroller.setSize(174, 194);
        listScroller.setViewportView(list);
        list.setLayoutOrientation(JList.VERTICAL);
        frame.getContentPane().add(listScroller);
    
        JList list_1 = new JList();
        list_1.setFont(new Font("Lucida Grande", Font.PLAIN, 21));
        list_1.setBounds(262, 41, 174, 194);
        list_1.setModel(model_list_1);
    
        JScrollPane listScroller1 = new JScrollPane();
        listScroller1.setLocation(262, 41);
        listScroller1.setSize(174, 194);
        listScroller1.setViewportView(list_1);
        list_1.setLayoutOrientation(JList.VERTICAL);
        frame.getContentPane().add(listScroller1);
    
        JButton shiftbutton = new JButton(">>>");
        shiftbutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                list.getSelectedValuesList().stream().forEach((data) -> {
                    model_list_1.addElement(data);
                    model_list.removeElement(data);
                });
            }
        });
        shiftbutton.setBounds(192, 116, 66, 52);
        frame.getContentPane().add(shiftbutton);
    }
    }
    
        

    提交回复
    热议问题