码迷,mamicode.com
首页 > 其他好文 > 详细

e742. 加入标签的可拖动能力

时间:2018-09-06 10:50:41      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:nsf   ESS   port   ati   span   example   its   strong   pad   

This example demonstrates how to modify a label component so that its text can be dragged and dropped to another component.

    // Create a label
    JLabel label = new JLabel("Label Text");
    
    // Specify that the label‘s text property be transferable; the value of
    // this property will be used in any drag-and-drop involving this label
    final String propertyName = "text";
    label.setTransferHandler(new TransferHandler(propertyName));
    
    // Listen for mouse clicks
    label.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            JComponent comp = (JComponent)evt.getSource();
            TransferHandler th = comp.getTransferHandler();
    
            // Start the drag operation
            th.exportAsDrag(comp, evt, TransferHandler.COPY);
        }
    });

 

Related Examples

e742. 加入标签的可拖动能力

标签:nsf   ESS   port   ati   span   example   its   strong   pad   

原文地址:https://www.cnblogs.com/borter/p/9596126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!