Groovy Swing Explorer文件放置处理程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Groovy Swing Explorer文件放置处理程序相关的知识,希望对你有一定的参考价值。

  1. import java.awt.datatransfer.*;
  2. import java.awt.dnd.*;
  3.  
  4. public class TFileDropHandler
  5. {
  6. // TFileDropHandler.groovy
  7. // Laurence Toenjes - 2010-08-18
  8. // Based on 1999 example from Cay Horstmann found at
  9. // http://java.itags.org/java-core-gui-apis/33457/
  10.  
  11. // dropped files closure example: { files -> println files };
  12. def TFileDropHandler( aSwingControl, Closure a_onDroppedFiles )
  13. { // ctor
  14. def dropTarget = new DropTarget( aSwingControl, new TFileDropTargetListener( a_onDroppedFiles ) )
  15. aSwingControl.setDropTarget( dropTarget )
  16. }
  17.  
  18. // --------------------------------------------------------
  19. class TFileDropTargetListener implements DropTargetListener
  20. {
  21. def Closure onDroppedFiles = null // our callback event
  22. public TFileDropTargetListener( a_onDroppedFiles ) { // ctor
  23. onDroppedFiles = a_onDroppedFiles }
  24.  
  25. public void dragEnter(DropTargetDragEvent event) {}
  26. public void dragExit(DropTargetEvent event) {}
  27. public void dropActionChanged(DropTargetDragEvent event) {}
  28. public void dragOver(DropTargetDragEvent event) {
  29. // provide visual feedback
  30. event.acceptDrag(DnDConstants.ACTION_COPY);
  31. }
  32.  
  33. if ( (event.getSourceActions() & DnDConstants.ACTION_COPY) != 0 ) {
  34. event.acceptDrop(DnDConstants.ACTION_COPY);
  35. }
  36. else {
  37. event.rejectDrop();
  38. return; // !!!!!!!!!!!!!!!!!!!!!
  39. }
  40. Transferable transferable = event.getTransferable();
  41. DataFlavor[] flavors = event.getCurrentDataFlavors();
  42. for (int i = 0; i < flavors.length; i++) {
  43. DataFlavor dataFlavor = flavors[i];
  44. try {
  45. // file flavor
  46. if ( dataFlavor.equals( DataFlavor.javaFileListFlavor ) ) {
  47. java.util.List fileList = (java.util.List) transferable.getTransferData(dataFlavor);
  48. def files = []
  49. fileList.each { files << it }
  50. this?.onDroppedFiles( files )
  51. event.dropComplete(true);
  52. }
  53. }
  54. event.dropComplete(false);
  55. }
  56. } // end for
  57. event.dropComplete(false);
  58. } // end drop
  59. }
  60. // --------------------------------------------------------
  61.  
  62. public static void main(String[] args) {
  63. // simple example
  64. def f = new javax.swing.JFrame()
  65. f.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE )
  66. f.size = [400,300]
  67. def ta = new javax.swing.JTextArea()
  68. f.add( ta )
  69.  
  70. new TFileDropHandler( ta, { files ->
  71. files.each {
  72. ta.append("${it} ")
  73. }
  74. } )
  75.  
  76. f.show()
  77. }
  78. }

以上是关于Groovy Swing Explorer文件放置处理程序的主要内容,如果未能解决你的问题,请参考以下文章

Groovy使用 SwingBuilder 构建 Swing 窗口 ( 创建一个 Swing 构造器 | 配置 Swing 窗口的一系列属性 | 设置 Swing 窗口显示 )

Groovy Swing builder Flickr示例

Groovy Swing递归组件

在新创建的对象上工作的Groovy Swing数据绑定示例(联机示例不适用)

groovy : poi 导出 Excel xlsx

Groovy Gradle创建目录