Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement intelligent drag and drop from one JTree to another?

I have two JTrees with some mock data in there, what I'm looking to do is to be able to take each 'Job' (15663-1, 15663-2, etc) and make a node for each, with a node for each part underneath it and the components attached to each part underneath that. In two trees, like this:

+------------------------------+------------------------------+
| PARTS TO BE SHIPPED          | SHIPPING BOX                 |
+------------------------------+------------------------------+
|[JOB]                         |[JOB]                         |
|+------[part]                 |+------[part]                | 
|        +------[component]    |        +------[component]    |
|        +------[component]    |        +------[component]    |
|+------[part]                 |+------[part]                 |
|        +------[component]    |        +------[component]    |
|[JOB]                         |[JOB]                         |
|+------[part]                 |+------[part]                 |
|        +------[component]    |        +------[component]    |
|        +------[component]    |        +------[component]    |
|+------[part]                 |+------[part]                 |
|        +------[component]    |        +------[component]    |
+------------------------------+------------------------------+

So that supposing I have two screws in the cover in job A in the 'parts to be shipped' jtree and I don't have anything in jobA in the shipping box, when I drag the screws over to the shipping box it should make an entry for jobA, make an entry for part A and make an entry for the component, then I want it to prompt for the quantity for that component and subtract that quantity from the parts to be shipped jtree.

So if I have a job called 1553-4 and it has a cover with four screws and I drag the screws into the shipping box then it should make an entry in the shipping box that says, "x screws" then prompt for the user to enter the number of screws they just packaged, if they package two screws then the jtree should change to reflect the 2 screws remaining for that job.

I have read a bunch of different drag and drop tutorials and I have some examples but I just cannot seem to get it. Any advice or help would be appreciated.

I know that I need to implement a TranferHandler but I'm not sure exactly how, there seems to be too much interface 'magic' going on and I'm really not understanding it.

This is what I have, I understand making nodes and such, here is what I have:

package com.protocase.examples;


import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

/**
 * @author DavidH
 */
public class JTreeExample {
    public static void main(String[] args) {
        addTreesAndDisplay();

    }

    private static void addTreesAndDisplay() throws HeadlessException {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();


        JTree tree = new JTree(getTreeModel());
        tree.setDragEnabled(true);
        tree.setPreferredSize(new Dimension(200,400));
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(tree);
        panel.add(scroll);


        JTree secondTree = new JTree(getTreeModel());
        secondTree.setPreferredSize(new Dimension(200,400));
        secondTree.setDragEnabled(true);
        JScrollPane secondScroll = new JScrollPane();
        secondScroll.setViewportView(secondTree);
        panel.add(secondScroll);


        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static DefaultTreeModel getTreeModel() {
        MutableTreeNode root =  new DefaultMutableTreeNode("15663-1");
        DefaultMutableTreeNode cover = new DefaultMutableTreeNode("Cover");
        DefaultMutableTreeNode base = new DefaultMutableTreeNode("Base");
        root.insert(cover, 0);
        root.insert(base, 0);
        cover.insert(new DefaultMutableTreeNode("2x PEMS"), 0);
        cover.insert(new DefaultMutableTreeNode("2x SCREWS"), 0);
        base.insert(new DefaultMutableTreeNode("4x SCREWS"), 0);
        base.insert(new DefaultMutableTreeNode("4x HANDLES"), 0);
        DefaultTreeModel model = new DefaultTreeModel(root);
        return model;
    }
}

I'm just looking for a concise drag and drop example of dragging into a JTree and dragging from a JTree.

like image 282
davidahines Avatar asked Mar 29 '12 21:03

davidahines


2 Answers

A very short, simple introduction to Drag and Drop in Swing in my own words and based on my own experience (which was mainly with drag-and-drop in JDK1.5, so new functionality might already be present).

There are two parts in a drag-and-drop operation. First there is the drag from the source component. The TransferHandler of the source component creates a Transferable, which is a container for the data which will be exchanged in the drag-and-drop operation. Depending on the data, there might be different representations of the data (which are called DataFlavors). For example if you drag-and-drop an URL to a text editor, it will most likely add the URL to the current document. But if you drop it onto a web-browser, you hope it opens that URL. So where the first is just interested in plain text, the second might be interested in a more complex object.

The second part is the drop. First it is decided whether the current location is a good drop target. It is up to the target component's transfer handler to decide whether it accepts the drop. Typically this is achieved by checking whether it can handle the data contained in the Transferable by asking the Transferable for the data for a specific DataFlavor (note: the Flavor must be known be both the source and target component). When it accepts the drop and the user releases the mouse, it can proceed with handling the data in the Transferable, and hopefully do something useful with it.

But as always, the Swing tutorials are a very good starting point. After you went through them, you can probably come up with a more detailed question (if you still have any, as your requirement is rather trivial)

like image 195
Robin Avatar answered Sep 23 '22 17:09

Robin


On theory, I think Robin has answered your question good. So below is the implementation that I've done. To summarize, the implementation include the top two label and bottom two scrollpanes, drag from left to the right. There are still minor thing like before import happen, a dialog should appear and asking user how much quantity to drop (and then do the arithmetic operation) but I think that can be your homework? ;-) Let me know if you need further help.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

public class JTreeExample extends JPanel
{
    private JTree tree;
    private DefaultTreeModel treeModel;


    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("My Warehouse");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTreeExample newContentPane = new JTreeExample();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);
    }

    public JTreeExample()
    {
        setLayout(new GridLayout(1, 3));
        JLabel lbl_parts = new JLabel("PARTS TO BE SHIPPED");       
        tree = new JTree(getTreeModel());
        tree.setDragEnabled(true);        
        tree.setPreferredSize(new Dimension(200,400));
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(tree);

        JLabel lbl_ship = new JLabel("SHIPPING BOX");
        treeModel = getTreeModel();
        JTree secondTree = new JTree(treeModel);
        secondTree.setPreferredSize(new Dimension(200,400));        
        secondTree.setTransferHandler(new TransferHandler() {

            @Override
            public boolean importData(TransferSupport support)
            {
                if (!canImport(support))
                {
                    return false;
                }

                JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();

                TreePath path = dl.getPath();
                int childIndex = dl.getChildIndex();

                String data;
                try
                {
                    data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
                }
                catch (UnsupportedFlavorException e)
                {
                    return false;                   
                }
                catch (IOException e)
                {
                    return false;                   
                }

                if (childIndex == -1)
                {
                    childIndex = tree.getModel().getChildCount(path.getLastPathComponent());
                }

                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(data);
                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
                treeModel.insertNodeInto(newNode, parentNode, childIndex);

                tree.makeVisible(path.pathByAddingChild(newNode));
                tree.scrollRectToVisible(tree.getPathBounds(path.pathByAddingChild(newNode)));

                return true;
            }

            public boolean canImport(TransferSupport support)
            {
                if (!support.isDrop())
                {
                    return false;                   
                }

                support.setShowDropLocation(true);
                if (!support.isDataFlavorSupported(DataFlavor.stringFlavor))
                {
                    System.err.println("only string is supported");
                    return false;                   
                }

                JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();

                TreePath path = dl.getPath();

                if (path == null)
                {
                    return false;                   
                }
                return true;
            }                       
        });
        JScrollPane secondScroll = new JScrollPane();
        secondScroll.setViewportView(secondTree);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(lbl_parts, BorderLayout.NORTH);
        topPanel.add(scroll, BorderLayout.CENTER);

        JPanel btmPanel = new JPanel(new BorderLayout());
        btmPanel.add(lbl_ship, BorderLayout.NORTH);
        btmPanel.add(secondScroll, BorderLayout.CENTER);

        add(topPanel);
        add(btmPanel);        

    }

    private static DefaultTreeModel getTreeModel()
    {
        MutableTreeNode root =  new DefaultMutableTreeNode("15663-1");                        

        DefaultMutableTreeNode cover = new DefaultMutableTreeNode("Cover");
        cover.insert(new DefaultMutableTreeNode("2x PEMS"), 0);
        cover.insert(new DefaultMutableTreeNode("2x SCREWS"), 0);
        root.insert(cover, 0);

        DefaultMutableTreeNode base = new DefaultMutableTreeNode("Base");
        base.insert(new DefaultMutableTreeNode("4x SCREWS"), 0);
        base.insert(new DefaultMutableTreeNode("4x HANDLES"), 0);
        root.insert(base, 0);

        DefaultTreeModel model = new DefaultTreeModel(root);
        return model;
    }
}
like image 36
Jasonw Avatar answered Sep 21 '22 17:09

Jasonw