将自定义对象从JList拖放到JLabel中

将自定义对象从JList拖放到JLabel中,第1张

将自定义对象从JList拖放到JLabel中

拖放可能是一个复杂的野兽,但可用的冲突信息并没有使它变得更容易。就个人而言,我喜欢避免使用Transfer API,但是我很老。

DnD的粘合剂确实是

DataFlavor
。我更喜欢自己动手,让生活更轻松。

在此示例中,我使用了一个

TransferHandler
,但实际上,您确实应该有一个拖放对象,特别是对于要拖放到的每个组件都有一个对象。

这样做的主要原因是,

canimport
如果您将鼠标拖到上,我会在方法中放入陷阱以拒绝它
JList
,因此您只能将其放在上
JLabel
,这是一个小技巧,可能不是最好的主意。

import java.awt.BorderLayout;import java.awt.Component;import java.awt.EventQueue;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.dnd.DnDConstants;import java.io.IOException;import javax.swing.DefaultListModel;import javax.swing.JComponent;import javax.swing.Jframe;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.TransferHandler;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class DnDTransferableTest {    public static void main(String[] args) {        new DnDTransferableTest();    }    public DnDTransferableTest() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     Jframe frame = new Jframe("Testing");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setLayout(new BorderLayout());     frame.add(new TestPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }    @SuppressWarnings("serial")    public class TestPane extends JPanel {        private JList<ListItem> list;        private JLabel label;        public TestPane() { list = new JList<ListItem>(); list.setDragEnabled(true); list.setTransferHandler(new ListTransferHandler()); DefaultListModel<ListItem> model = new DefaultListModel<ListItem>(); for (int index = 0; index < 10; index++) {     model.addElement(new ListItem("Item " + index)); } list.setModel(model); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.weighty = 1; gbc.weightx = 1; gbc.fill = GridBagConstraints.BOTH; add(new JScrollPane(list), gbc); label = new JLabel("Drag on me..."); gbc.gridx++; gbc.weightx = 1; gbc.fill = GridBagConstraints.NONE; add(label, gbc); label.setTransferHandler(new ListTransferHandler());        }    }    @SuppressWarnings("serial")    public class ListTransferHandler extends TransferHandler {        @Override        public boolean canimport(TransferSupport support) { return (support.getComponent() instanceof JLabel) && support.isDataFlavorSupported(ListItemTransferable.LIST_ITEM_DATA_FLAVOR);        }        @Override        public boolean importData(TransferSupport support) { boolean accept = false; if (canimport(support)) {     try {         Transferable t = support.getTransferable();         Object value = t.getTransferData(ListItemTransferable.LIST_ITEM_DATA_FLAVOR);         if (value instanceof ListItem) {  Component component = support.getComponent();  if (component instanceof JLabel) {      ((JLabel)component).setText(((ListItem)value).getText());      accept = true;  }         }     } catch (Exception exp) {         exp.printStackTrace();     } } return accept;        }        @Override        public int getSourceActions(JComponent c) { return DnDConstants.ACTION_COPY_OR_MOVE;        }        @Override        protected Transferable createTransferable(JComponent c) { Transferable t = null; if (c instanceof JList) {     @SuppressWarnings("unchecked")     JList<ListItem> list = (JList<ListItem>) c;     Object value = list.getSelectedValue();     if (value instanceof ListItem) {         ListItem li = (ListItem) value;         t = new ListItemTransferable(li);     } } return t;        }        @Override        protected void exportDone(JComponent source, Transferable data, int action) { System.out.println("ExportDone"); // Here you need to decide how to handle the completion of the transfer, // should you remove the item from the list or not...        }    }    public static class ListItemTransferable implements Transferable {        public static final DataFlavor LIST_ITEM_DATA_FLAVOR = new DataFlavor(ListItem.class, "java/ListItem");        private ListItem listItem;        public ListItemTransferable(ListItem listItem) { this.listItem = listItem;        }        @Override        public DataFlavor[] getTransferDataFlavors() { return new DataFlavor[]{LIST_ITEM_DATA_FLAVOR};        }        @Override        public boolean isDataFlavorSupported(DataFlavor flavor) { return flavor.equals(LIST_ITEM_DATA_FLAVOR);        }        @Override        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { return listItem;        }    }    public static class ListItem {        private String text;        public ListItem(String text) { this.text = text;        }        public String getText() { return text;        }        @Override        public String toString() { return getText();        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5176141.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存