Вопрос

In this program I want to copy items from List1 to List2 randomly as well as vies versa. However instead of selected element, it moves different elements to opposite JList. Initially when i copy from list1 to list2, it works well. But when i copy again from List2 to List1, it doesn't work right. Can someone help me to understand what is the wrong here?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyFrame extends JFrame{

    private JLabel lTitle;
    private JList list1;
    private JButton btAdd1;
    private JButton btAdd2;
    private JPanel buttonpane;
    private JList list2;
    private JPanel titlePane;
    private JPanel listPane;
    private DefaultListModel dlm2;
    private String data1[];
    private String data2[];
    private DefaultListModel dlm1;

    MyFrame(){
        setSize(200,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        lTitle=new JLabel("Subject List");
        lTitle.setFont(new Font("",1,30));

        data1=new String[]{"A1","A2","A3","A4","A5"};
        data2=new String[]{"B1","B2","B3","B4","B5"};

         dlm1=new DefaultListModel();
         list1=new JList(dlm1);
          dlm2=new DefaultListModel();
         list2=new JList(dlm2);

        for(int i=0;i<data1.length;i++){
            dlm1.add(i,data1[i]);
        }   

        for(int i=0;i<data2.length;i++){
            dlm2.add(i,data2[i]);
        }   

        btAdd1=new JButton(">>");
        btAdd1.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                int x[]=list1.getSelectedIndices();
                for(int i=0;i<x.length;i++){
                    dlm2.add(i,data1[x[i]]);
                }
            }       
        });

        btAdd2=new JButton("<<");
        btAdd2.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                int x[]=list2.getSelectedIndices();
                for(int i=0;i<x.length;i++){
                    dlm1.add(i,data2[x[i]]);
                }
            }       
        });

        buttonpane=new JPanel(new GridLayout(2,1,10,10));
        buttonpane.add(btAdd1);
        buttonpane.add(btAdd2);
        titlePane=new JPanel( );
        titlePane.add(lTitle);
        listPane=new JPanel();
        listPane.add(list1);
        listPane.add(buttonpane);
        listPane.add(list2);

        add("North",titlePane);
        add("Center",listPane);

    }
}

class Demo{
    public static void main(String args[]){
        new MyFrame().setVisible(true);     
    }
}
Это было полезно?

Решение

This is because you try to get the values to copy from the original data1 and data2 arrays of Strings, but these arrays are not updated when you copy the new values, therefore you can not use them anymore (if you want to keep this design).

Instead you can access the data through the DefaultListModel objects: they are updated with the new copied values. Basically you have to modify the two listeners, for the first button:

btAdd1.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        int x[]=list1.getSelectedIndices();
        for(int i=0;i<x.length;i++){
            dlm2.add(i,dlm1.get(x[i]));
        }
    }       
});

And also for the second button:

btAdd2.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        int x[]=list2.getSelectedIndices();
        for(int i=0;i<x.length;i++){
            dlm1.add(i,dlm2.get(x[i]));
        }
    }       
});

I hope this will help you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top