JList
我正在尝试使用两个实例在 java 中实现拖放。基本流程工作正常。但是,当我从一个列表中拖动一个字符串时,我只想将放置目标限制为第二个列表。我注意到,当我将一个字符串从一个列表拖到我的桌面时,它会创建一个包含该字符串的文件。
有没有办法避免这种情况?
public class SampleDnD extends JFrame{
public static void main(String[] args) {
new SampleDnD();
}
/**
* new form of frame sample contain 2 JLists with drag enabled.
*/
public SampleDnD(){
JList l1 = new JList();
JList l2 = new JList();
JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
DefaultListModel listModel1 = new DefaultListModel();
DefaultListModel listModel2 = new DefaultListModel();
String[] list1 = new String[]{"1","3","5","7","9"};
String [] list2 = new String[]{"0","2","4","6","8"};
for(int index=0;index<list1.length;index++){
listModel1.add(index, list1[index]);
listModel2.add(index, list2[index]);
}
l1.setModel(listModel1);
l2.setModel(listModel2);
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
l1.setDropMode(DropMode.INSERT);
l1.setDragEnabled(true);
l2.setDragEnabled(true);
l1.setTransferHandler(new ListTransferHandler());
l2.setDropMode(DropMode.INSERT);
l2.setTransferHandler(new ListTransferHandler());
jScrollPane1.setViewportView(l1);
jScrollPane2.setViewportView(l2);
Container cp = getContentPane();
cp.setLayout(new BoxLayout(cp,BoxLayout.Y_AXIS));
setPreferredSize(new Dimension(800,500));
getContentPane().add(jScrollPane1);
getContentPane().add(jScrollPane2);
setVisible(true);
pack();
}
public class ListTransferHandler extends TransferHandler {
/**
* We only support importing strings.
*/
public boolean canImport(TransferHandler.TransferSupport info) {
// Check for String flavor
if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
return false;
}
return true;
}
/**
* Bundle up the selected items in a single list for export.
*
*/
protected Transferable createTransferable(JComponent c) {
JList list = (JList)c;
String value = (String)list.getSelectedValue();
return new StringSelection(value);
}
/**
* We support only move actions.
*/
public int getSourceActions(JComponent c) {
return TransferHandler.MOVE;
}
/**
* Perform the actual import. This demo only supports drag and drop.
*/
public boolean importData(TransferHandler.TransferSupport info) {
if (!info.isDrop()) {
return false;
}
JList list = (JList)info.getComponent();
DefaultListModel listModel = (DefaultListModel)list.getModel();
JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
int index = dl.getIndex();
boolean insert = dl.isInsert();
// Get the string that is being dropped.
Transferable t = info.getTransferable();
String data;
try {
data = (String)t.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception e) { return false; }
if (insert) {
listModel.add(index++, data);
} else {
// If the items go beyond the end of the current
// list, add them in.
if (index < listModel.getSize()) {
listModel.set(index++, data);
} else {
listModel.add(index++, data);
}
}
return true;
}
/**
* Remove the items moved from the list.
*/
protected void exportDone(JComponent c, Transferable data, int action) {
JList source = (JList)c;
DefaultListModel listModel = (DefaultListModel)source.getModel();
if(action == TransferHandler.MOVE)
{
listModel.remove(source.getSelectedIndex());
}
}
}
}