简体   繁体   中英

one JList triggering addition of data to another JList

here is the code, that anyone can run easy and fast, that shows my problem:

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class Driver extends JFrame {

    public ArrayList<String> RN = new ArrayList<String>();

    private JPanel contentPane;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    Driver frame = new Driver();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Driver() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 605, 497);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        DefaultListSelectionModel m = new DefaultListSelectionModel();
        m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m.setLeadAnchorNotificationEnabled(false);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(26, 133, 538, 133);
        contentPane.add(scrollPane);
        scrollPane.setViewportView(listRN);
        listRN.setSelectionModel(m);
    //  listRN.setCellRenderer(new MyListCellRenderer());
        listRN.addMouseListener(mouseListener);
        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(26, 296, 538, 133);
        contentPane.add(scrollPane_1);



        scrollPane_1.setViewportView(listPL);
        listPL.setSelectionModel(m);
    //  listPL.setCellRenderer(new MyListCellRenderer());
        JButton btnNewButton = new JButton("Load");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                RN.add("Ivan");
                RN.add("Gaga");
                RN.add("Dragan");
                for(int i=0;i<RN.size();i++){
                    modelRN.addElement(RN.get(i));
                }

            }
        });
        btnNewButton.setBounds(386, 58, 89, 23);
        contentPane.add(btnNewButton);
    }

    MouseListener mouseListener = new MouseAdapter() {
         public void mouseClicked(MouseEvent e) {
         String d = null;

             if (e.getClickCount() == 1) {

                 int index = listRN.locationToIndex(e.getPoint());
                 System.out.println("clicked on Item " + index);

                  d = listRN.getSelectedValue();

                 modelPL.addElement(d);
                 }

              }


     };


     DefaultListModel<String> modelRN = new DefaultListModel<String>();

     DefaultListModel<String> modelPL = new DefaultListModel<String>();

     JList<String> listRN = new JList<String>(modelRN); 

     JList<String> listPL = new JList<String>(modelPL);

}

Its a simple example. Click on Load button and strings are added to first JList. Click on a string in that JList and it is loaded to the second JList, so nothing major. My problem is with the behavior of the JLists after that...when I click on a selection in a Jlist it loads the string to the second Jlist but the selection highlighted jumps to another String...plus when ever i click on a string in the second Jlist it is connected to the first one.

What is my fundamental flaw in understanding JList behavior?

listPL和listRN不应具有相同的ListSelectionModel,当您在第一个列表中选择一个项目时,它会添加到第二个列表中,并且其中的所选项目也会更改,因此第一个列表中的所选项目也会更改,因为它们具有相同的ListSelectionModel 。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM