0

いくつかの文字列値を持つモデルがあります。このモデルを 2 つの jlist に適用します。あるjlistから値をクリックするたびに、その値が別のjlistから消える必要があります。次に、他の jlist に発生した場合も同じですが、最初に値をモデルに含まれている値に更新する必要があります。私はいくつかの努力をしましたが、コードを使用して1つの値をクリックすると、両方のリストで消えます! 私は何を間違っていますか?コードは次のとおりです。

 /*
 * 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;
        ...
    }

要素fromModelを移動するにはtoModel

    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);
                    }
                });
            }
        }
    };

要素toModelを移動するにはfromModel

    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

You are using twice the same model but they are actually different. Use different models if they are intrinsically different. It is only normal that if you modify a model used by different views (the JList) they both reflect the changes on the underlying model.

于 2012-04-30T16:22:47.077 に答える