import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.UIManager; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * TestTree.java * * Created on Jun 3, 2009, 1:08:08 PM */ /** * * @author jacobenn */ public class TestTree extends javax.swing.JFrame { /** Creates new form Aonori_Test_GUI */ public TestTree(){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ex) { Logger.getLogger(TestTree.class.getName()).log(Level.SEVERE, null, ex); } initComponents(); treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("New Root Item"); jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1)); jTree1.setEditable(true); jScrollPane1.setViewportView(jTree1); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTree1 = new javax.swing.JTree(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTree1.setDragEnabled(true); jTree1.setEditable(true); jTree1.setRootVisible(false); jTree1.setShowsRootHandles(true); jScrollPane1.setViewportView(jTree1); jButton1.setText("Delete"); jButton1.setPreferredSize(new java.awt.Dimension(60, 20)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Add Child"); jButton2.setPreferredSize(new java.awt.Dimension(60, 20)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Add Root"); jButton3.setPreferredSize(new java.awt.Dimension(60, 20)); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { TreePath currentSelection = jTree1.getSelectionPath(); if (currentSelection != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) currentSelection.getLastPathComponent(); DefaultTreeModel model = ((DefaultTreeModel) jTree1.getModel()); model.removeNodeFromParent(node); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { TreePath currentSelection = jTree1.getSelectionPath(); if (currentSelection != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) currentSelection.getLastPathComponent(); DefaultTreeModel model = ((DefaultTreeModel) jTree1.getModel()); node.add(new DefaultMutableTreeNode("New Item")); model.nodeStructureChanged(node); } } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { String temp = treeNode1.getRoot().toString(); javax.swing.tree.DefaultMutableTreeNode treeNode = new javax.swing.tree.DefaultMutableTreeNode(temp); treeNode1.add(treeNode); jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1)); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TestTree().setVisible(true); } }); } javax.swing.tree.DefaultMutableTreeNode treeNode1; // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTree jTree1; // End of variables declaration }
Register and join over 40,000 other developers!
Recent Topics
-
Tekken 7
IsrealJones - Mar 22 2021 07:21 AM
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

1 reply to this topic
#1
Posted 03 June 2009 - 01:12 PM
I am having some trouble trying to understand this concept. I am relatively new to Java and I am trying to tackle something new, GUI. I have made a simple tree with three buttons. I am trying to add the ability to drag one node from one location in the tree to another location and drop it there. I feel certain that I have run across the exact article I need to do this found here, but I am afraid that it is just a bit over my head. I wonder if anyone will be able to translate or better explain what I need to do to my code to accomplish this task. Any and all help you can provide is welcomed. The following is code that I have made in Netbeans GUI builder.
#2
Posted 11 June 2009 - 02:51 PM
I did this.
BM
package ICONS; import java.awt.Point; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragGestureRecognizer; import java.awt.dnd.DragSource; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetContext; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; public class DragNDrop implements DragGestureListener, DropTargetListener { DropTarget target; JTree sourceTree; DragSource source = new DragSource(); DragGestureRecognizer recognizer; DefaultMutableTreeNode subTreeDuplicate = null; DefaultMutableTreeNode startPoint = null; public DragNDrop(JTree tree, int actions) { target = new DropTarget(tree, this); sourceTree = tree; recognizer = source.createDefaultDragGestureRecognizer(sourceTree, actions, this); } public void dragEnter(DropTargetDragEvent dtde) { } public void dragOver(DropTargetDragEvent dtde) { } public void dragExit(DropTargetEvent dte) { } public void dragGestureRecognized(DragGestureEvent dge) { TreePath path = sourceTree.getSelectionPath(); if ((path == null) || (path.getPathCount() <= 1) || ((DefaultMutableTreeNode) path.getLastPathComponent()).isRoot()) { return; } else { try { subTreeDuplicate = recursiveTreeTools.analyzeTreePath(path); } catch (CloneNotSupportedException ex) { Logger.getLogger(DragNDrop.class.getName()).log(Level.SEVERE, null, ex); } startPoint = (DefaultMutableTreeNode) path.getLastPathComponent(); } } public void dropActionChanged(DropTargetDragEvent dtde) { } public void drop(DropTargetDropEvent dtde) { DefaultMutableTreeNode dropPoint; if (startPoint != null && subTreeDuplicate != null) { Point pt = dtde.getLocation(); DropTargetContext dtc = dtde.getDropTargetContext(); JTree tree = (JTree) dtc.getComponent(); TreePath parentpath = tree.getClosestPathForLocation(pt.x, pt.y); dropPoint = (DefaultMutableTreeNode) parentpath.getLastPathComponent(); if (!dropPoint.equals(startPoint) && !startPoint.isNodeDescendant(dropPoint)) { DefaultTreeModel model = ((DefaultTreeModel) tree.getModel()); dropPoint.add(subTreeDuplicate); model.removeNodeFromParent(startPoint); model.nodeStructureChanged(dropPoint); } } startPoint = null; subTreeDuplicate = null; } }That is the main portion of what needs to be done. Also enable some basic stuff like this under the initComponents():
jTree1.setAutoscrolls(true); jTree1.setDragEnabled(true); jTree1.setEditable(true); jTree1.setShowsRootHandles(true); jScrollPane1.setViewportView(jTree1); ROOT = new javax.swing.tree.DefaultMutableTreeNode("Root"); jTree1.setModel(new javax.swing.tree.DefaultTreeModel(ROOT)); ds = new DragNDrop(jTree1, DnDConstants.ACTION_COPY_OR_MOVE);If anyone has any improvements, I am all eyes.
BM
Also tagged with one or more of these keywords: setpreferredsize
Language Forums →
Java →
Grid With MysqlStarted by Apprentice123, 30 Jul 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
Really Easy Paintcomponent QuestionStarted by Cruel Hand, 07 Jul 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
[Solved] Bouncing Ball HelpStarted by Cruel Hand, 17 Jun 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
[Solved]Help Plotting A Function On A GraphStarted by Cruel Hand, 07 Jun 2012 ![]() |
|
![]() |
||
Language Forums →
Java →
Bar Graph HelpStarted by Cruel Hand, 06 Jun 2012 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download