Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the drag and drop function in Java Swing programming

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use the drag-and-drop function in Java Swing programming, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

The drag-and-drop function is actually like holding down CTRL and then dragging an icon when we use windows to copy the object. This provides users with a great user experience, there is no way for programmers to make trouble for themselves and make users feel convenient, in fact, this function AWT also provides swimming words that just take advantage of this, because it has nothing to do with the interface.

DropTarget (drag and drop destination) eg (display after dragging the picture):

Public class TestDropTarget {final int DESKTOP_WIDTH = 480; final int DESKTOP_HEIGHT = 360; final int FRAME_DISTANCE = 30; JFrame jf = new JFrame ("Test the drag and drop target-drag the picture file into this window"); / / define a virtual desktop private JDesktopPane desktop = new JDesktopPane (); / / Save the coordinate point private int nextFrameX; private int nextFrameY of the next internal window / / define the size of the internal window as virtual desktop private int width = DESKTOP_WIDTH / 2; private int height = DESKTOP_HEIGHT / 2; public void init () {desktop.setPreferredSize (new Dimension (DESKTOP_WIDTH, DESKTOP_HEIGHT)); / / create the current window as a drag and drop source new DropTarget (jf, DnDConstants.ACTION_COPY, new ImageDropTargetListener ()) Jf.add (desktop); jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.pack (); jf.setVisible (true);} class ImageDropTargetListener extends DropTargetAdapter {public void drop (DropTargetDropEvent event) {/ / accept copy operation event.acceptDrop (DnDConstants.ACTION_COPY_OR_MOVE) / / get the drag-and-drop content Transferable transferable = event.getTransferable (); DataFlavor [] flavors = transferable.getTransferDataFlavors (); / / traverse all data formats for (int I = 0; I) in the drag-and-drop content

< flavors.length; i++) { DataFlavor d = flavors[i]; try { //如果拖放内容的数据格式是文件列表 if (d.equals(DataFlavor.javaFileListFlavor)) { //取出拖放操作里的文件列表 java.util.List fileList = (java.util.List) transferable.getTransferData(d); for (Object f : fileList) { //显示每个文件 showImage((File)f , event); } } } catch (Exception e) { e.printStackTrace(); } //强制拖放操作结束,停止阻塞拖放源 event.dropComplete(true); } } //显示每个文件的工具方法 private void showImage(File f , DropTargetDropEvent event)throws java.io.IOException { Image image = ImageIO.read(f); if (image == null) { //强制拖放操作结束,停止阻塞拖放源 event.dropComplete(true); JOptionPane.showInternalMessageDialog(desktop , "系统不支持这种类型的文件"); //方法返回,不会继续操作 return; } ImageIcon icon = new ImageIcon(image); //创建内部窗口显示该图片 JInternalFrame iframe = new JInternalFrame(f.getName() ,true , true , true , true); JLabel imageLabel = new JLabel(icon); iframe.add(new JScrollPane(imageLabel)); desktop.add(iframe); //设置内部窗口的原始位置(内部窗口默认大小是0X0,放在0,0位置) iframe.reshape(nextFrameX, nextFrameY, width, height); //使该窗口可见,并尝试选中它 iframe.show(); //计算下一个内部窗口的位置 nextFrameX += FRAME_DISTANCE; nextFrameY += FRAME_DISTANCE; if (nextFrameX + width >

Desktop.getWidth () nextFrameX = 0; if (nextFrameY + height > desktop.getHeight ()) nextFrameY = 0;}} public static void main (String [] args) {new TestDropTarget (). Init ();}}

DropSource (drag and drop source) eg (drag text into a text editor that supports drag and drop, Editplus,Eclipse,idea, etc. Notepad, UltraEdit actually does not support) drag and drop source is mainly to drag and drop objects into Transferable objects

Public class TestDragSource {JFrame jf = new JFrame ("drag and drop support for Swing"); JLabel srcLabel = new JLabel ("drag and drop support for AWT. / n" + "drag the contents of this text field into other programs. / n"); public void init () {DragSource dragSource = DragSource.getDefaultDragSource () / / convert srcLabel to drag and drop source It can accept both copy and move operations: dragSource.createDefaultDragGestureRecognizer (srcLabel, DnDConstants.ACTION_COPY_OR_MOVE, new DragGestureListener () {public void dragGestureRecognized (DragGestureEvent event) {/ / wraps the text message in JLabel into a Transferable object String txt = srcLabel.getText () Transferable transferable = new StringSelection (txt); / / continue the drag and drop operation, using the hand cursor event.startDrag (Cursor.getPredefinedCursor (Cursor.HAND_CURSOR), transferable);}}); jf.add (new JScrollPane (srcLabel)) Jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.pack (); jf.setVisible (true);} public static void main (String [] args) {new TestDragSource (). Init ();}}

JDK1.4 began to simplify the drag-and-drop function of Swing. Different components provide drag-and-drop sources and drag-and-drop targets. Here we talk about the most frequently used text field. It is very convenient for him to drag text into and out of the text.

However, this feature is not enabled by default, so call the setDragEnabled method manually to make it take effect. Eg

Public class SwingDndSupport {JFrame jf = new JFrame ("drag and drop support for Swing"); JTextArea srcTxt = new JTextArea (8,30); JTextField jtf = new JTextField (34); public void init () {srcTxt.append ("drag and drop support for AWT. / n"); srcTxt.append ("drag the contents of this text domain into other programs. / n") / / launch drag and drop support for text fields and single-line text boxes: srcTxt.setDragEnabled (true); jtf.setDragEnabled (true); jf.add (new JScrollPane (srcTxt)); jf.add (jtf, BorderLayout.SOUTH); jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.pack (); jf.setVisible (true) } public static void main (String [] args) {new SwingDndSupport () .init ();}}

Is it convenient? that's all for today.

About how to use the drag-and-drop function in Java Swing programming is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report