English 中文(简体)
具有相同模式的jlists的配对(如何从另一个选定的名单中删除)
原标题:Pair of jlists with the same model (How to remove from one the selected of other)

I have a model with some string values.This model, I apply it to two jlists. I need every time that I click from one jlist a value, that value to dissapear from the other. Then the same if it happents to the other jlist but first the values must be updated to those the model contains. I made some effort but with my code when I click one value then it dissapears on both lists! What am I doing wrong? Here is the code:

 /*
 * 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 φάκελο για να γίνει αντιγραφή εγγράφων.
Το παράθυρο θα       κλείσει.", "Λάθος", 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 
最佳回答

每一份清单都需要使用其自己的参考资料<代码>ListModel。

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

移转要素<条码>从Model到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);
                    }
                });
            }
        }
    };

移转要素<条码>至Model至<条码>

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

    };


}

我希望这能够帮助你

问题回答

你们使用的是同一模式的两倍,但实际上却有所不同。 如果使用不同的模式本质上有所不同,则使用不同的模式。 如果你修改不同观点(JList)使用的模式,这只是正常的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签