0

我有一个带有一些字符串值的模型。这个模型,我将它应用于两个 jlist。每次我从一个 jlist 中单击一个值时,我都需要该值从另一个值中消失。如果它发生在另一个 jlist 上,则相同,但首先必须将值更新为模型包含的值。我做了一些努力,但是当我单击一个值时,我的代码在两个列表中都消失了!我究竟做错了什么?这是代码:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package accessfiletest;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;


/**
 *
* @author 
*/

 @SuppressWarnings("serial")
public class MoveFolders extends JFrame  
{
//start of class MoveFolders 
//start of variables
private DefaultListModel<String> theModel;
private DefaultListModel<String> fromModel;
private DefaultListModel<String> toModel;
private JList<String> fromJList;
private JList<String> toList;
private JButton moveButton;
private JPanel theJPanel;
//end of variables
public MoveFolders( DefaultListModel<String> model1)
{
 super("Μετακίνηση Εγγράφων από Φάκελο σε Φάκελο");
 fromModel=model1;
 toModel=model1;
 theModel=model1;
 theJPanel=new JPanel(null);
 fromJList=new JList<>(fromModel);
 fromJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 fromJList.setSelectedIndex(0);
 fromJList.addMouseListener(fromlistener);

JScrollPane frompane=new  JScrollPane(fromJList);
frompane.setBounds(50, 50, 200, 150);
theJPanel.add(frompane);
moveButton=new JButton("ΜΕΤΑΚΙΝΗΣΗ >>");
moveButton.setBounds(260, 90, 150, 20);
theJPanel.add(moveButton);
toList=new JList<>(toModel);
 if (model1.getSize()>1)
   {
    toList.setSelectedIndex(1);

   }
else
   {
    JOptionPane.showMessageDialog(null,
"Πρέπει να έχετε πάνω από 1 φάκελο για να γίνει αντιγραφή εγγράφων.\nΤο παράθυρο θα       κλείσει.", "Λάθος", JOptionPane.ERROR_MESSAGE);
 dispose();
   }
 toList.addMouseListener(toListener);
 toList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 JScrollPane topane=new  JScrollPane(toList);
 topane.setBounds(420, 50, 200, 150);
 theJPanel.add(topane);

 add(theJPanel);
 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 setSize(670, 300);
 setVisible(true);
 }

 MouseListener fromlistener = new MouseAdapter() {
 public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        final int index = fromJList.locationToIndex(e.getPoint());
       SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                   toModel=theModel;
                   toModel.remove(index);
                }
            });
        }
   }
};

MouseListener toListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        final int index = fromJList.locationToIndex(e.getPoint());
        SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    fromModel=theModel;
                    fromModel.remove(index);
                }
            });
     }
   }

  };

}//end of class MoveFolders 
4

2 回答 2

2

每个列表都需要使用自己的引用ListModel

    public MoveFolders(DefaultListModel<String> model1) {
        ...
        fromModel = new DefaultListModel<>();
        for (Object obj : model1.toArray()) {
            fromModel.addElement((String) obj);
        }
        toModel = new DefaultListModel<>();
        theModel = model1;
        ...
    }

将元素移动fromModeltoModel

    MouseListener fromlistener = new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1) {
                final int index = fromJList.locationToIndex(e.getPoint());
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        int index = fromJList.getSelectedIndex();
                        toModel.addElement(fromModel.getElementAt(index));
                        fromModel.remove(index);
                    }
                });
            }
        }
    };

将元素移动toModelfromModel

    MouseListener toListener = new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1) {
                final int index = fromJList.locationToIndex(e.getPoint());
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        int index = toList.getSelectedIndex();
                        fromModel.addElement(toModel.getElementAt(index));
                        toModel.remove(index);
                    }
                });
            }
        }

    };


}

我希望这可以帮助你

于 2012-04-30T16:28:41.560 回答
1

您使用了两次相同的模型,但它们实际上是不同的。如果它们本质上不同,请使用不同的模型。如果您修改由不同视图(JList)使用的模型,它们都会反映底层模型的更改,这是很正常的。

于 2012-04-30T16:22:47.077 回答