Example usage for javax.swing DropMode USE_SELECTION

List of usage examples for javax.swing DropMode USE_SELECTION

Introduction

In this page you can find the example usage for javax.swing DropMode USE_SELECTION.

Prototype

DropMode USE_SELECTION

To view the source code for javax.swing DropMode USE_SELECTION.

Click Source Link

Document

A component's own internal selection mechanism (or caret for text components) should be used to track the drop location.

Usage

From source file:DnDDemo3.java

public static void main(String[] args) {
    JPanel north = new JPanel();
    north.add(new JLabel("Drag from here:"));
    JTextField field = new JTextField(10);
    field.setDragEnabled(true);/*  www  .  j a v  a 2 s .  c  o  m*/
    north.add(field);

    final DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("first");
    listModel.addElement("second");
    final JList list = new JList(listModel);
    list.setDragEnabled(true);

    list.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }
            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            } else {
                return true;
            }
        }

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

            Transferable transferable = support.getTransferable();
            String data;
            try {
                data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) support.getDropLocation();
            int index = dl.getIndex();
            if (dl.isInsert()) {
                listModel.add(index, data);
            } else {
                listModel.set(index, data);
            }
            Rectangle r = list.getCellBounds(index, index);
            list.scrollRectToVisible(r);
            return true;
        }
    });
    JScrollPane center = new JScrollPane();
    center.setViewportView(list);

    list.setDropMode(DropMode.USE_SELECTION);
    JPanel cp = new JPanel();
    cp.setLayout(new BorderLayout());
    cp.add(north, BorderLayout.NORTH);
    cp.add(center, BorderLayout.CENTER);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);
}

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);//  www .  ja v a 2s .c  om
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.USE_SELECTION);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:DropDemo.java

public void actionPerformed(ActionEvent ae) {
    Object val = dropCombo.getSelectedItem();
    if (val == "USE_SELECTION") {
        list.setDropMode(DropMode.USE_SELECTION);
    } else if (val == "ON") {
        list.setDropMode(DropMode.ON);
    } else if (val == "INSERT") {
        list.setDropMode(DropMode.INSERT);
    } else if (val == "ON_OR_INSERT") {
        list.setDropMode(DropMode.ON_OR_INSERT);
    }/*  w ww. jav a2  s.  c  o  m*/
}