Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic JTree from database query,how 2 build JTree wth dynamic hierar 1

Status
Not open for further replies.

pjeypal

Programmer
Apr 17, 2006
36
US
Hi,

Can anyone help me with a code for dynamically create a JTree hierarchy using data retrieved from an oracle table.

Thanks,
Priya
 
Hi,
Thanks for the reply.I am comfortable with the first part (a).
I need guidance with the second part (b)
The table has 3 fields location, description and value
location is the node and other two are leaves
There are over 4000 records in the table.
The query is a simple select query to retrieve all records.
I need to display all these records in the format mentioned above.I'm not clear about how to do this.
node
/\
leaf leaf
Any help appreciated

Thanks,
Priya
 
Those 4000 records aren't related to each other?
There is one root with 4000 childs (=nodes), and each has 2 leaves?

That doesn't sound like a datastructure which is meant to be displayed in a JTree, but who am I to judge.

Code:
 import javax.swing.tree.*;
import javax.swing.*;
import java.awt.*;

class MyNode extends DefaultMutableTreeNode
{
	private String location;
	
	public MyNode (String location, String description, int value)
	{
		this.location = location;
		add (new DescriptionNode (description));
		add (new ValueNode (value));
	}
	
	public String toString ()	{	return location;	}
}

class DescriptionNode extends DefaultMutableTreeNode
{
	private String description;
	public DescriptionNode (String description)
	{
		this.description = description;
	}
	public String toString ()	{	return description;	}
}

class ValueNode extends DefaultMutableTreeNode
{
	private int value;
	public ValueNode (int value)
	{
		this.value = value;
	}
	public String toString ()	{	return ""+value;	}
}

public class SmallTree extends JFrame // implements ActionListener
{
	private JTree jtree;

	public SmallTree ()
	{
		super ("Treedemo");
		JPanel mainpanel = new JPanel ();
		mainpanel.setLayout (new BorderLayout ());
		this.getContentPane ().add (mainpanel);
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode ();
		for (int i = 0; i < 8; ++i)
		{
			DefaultMutableTreeNode  mtn = new MyNode (""+i, " desc. " + i, i);
			root.add (mtn);
		}
		jtree = new JTree (root);
		mainpanel.add (jtree, BorderLayout.CENTER);
		setSize (400, 400);
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		setVisible (true);
	}

	public static void main (String args[])
	{
		new SmallTree ();
	}
}


seeking a job as java-programmer in Berlin:
 
Hi,
Thanks a lot for the code.Regarding the base table,I need to display two fields from each record(not 3 as i had mentioned earlier) location and description.This is not a normal tree with root, node and leaf(slightly different representation needed by client).I need to display records in a hierarchial manner with location and description side by side something like this

location1 desc
|

location2 desc
|

location3 desc
|
.
.
.
.
location4000 desc

Need to have drag and drop functionality for this tree with another similiar hierarchy and description should be editable with the database being updated with changes.

Does this make any sense to you.Do you have any suggestions on how to go about this.

Thanks,
Priya

 
Hi,
The code which u sent me was quite useful.Do you have any similiar ones for drag and drop from one tree to another

Priya
 
Still looking for a solution?

I tried for days and didn't get anything up and running. Then I decided to "fake" the drag&drop mechanism and only use the parts I fully understand and that are working without wasting valuable nights.

1. You need a drag&drop-Listener
---------------------------------

public class DnDListener
implements DropTargetListener, DragSourceListener
{ int xpos, ypos;

public DnDListener()
{
}

// drag
public void dragEnter(DragSourceDragEvent de)
{ System.out.println("Drag enter..."+de.getSource().toString()); }

public void dragExit(DragSourceEvent de)
{ System.out.println("Drag exit..."); }

public void dragOver(DragSourceDragEvent de)
{ System.out.println("Drag over..."); }

public void dropActionChanged(DragSourceDragEvent de)
{ System.out.println("Drag action changed..."); }

public void dragDropEnd(DragSourceDropEvent de)
{ System.out.println("Drag drop..."); }

//drop
public void dragEnter(DropTargetDragEvent de)
{ System.out.println("Drop enter..."+( de.getSource().toString() ) ); }

public void dragExit(DropTargetEvent de)
{ System.out.println("Drop exit..."); }

public void dragOver(DropTargetDragEvent de)
{ System.out.println("Drop over..."); }

public void dropActionChanged(DropTargetDragEvent de)
{ System.out.println("Drop action changed..."); }

public void drop(DropTargetDropEvent de)
{ System.out.println("Drop drop..."+( de.getTransferable().toString() ) ); }

}


2. You need a mouse listener
-----------------------------

ml = new MouseListener()
{
public void mousePressed(MouseEvent e)
{
JComponent c = (JComponent)e.getSource();
TransferHandler th = c.getTransferHandler();
th.exportAsDrag(c, e, TransferHandler.COPY);
System.out.println("\n...DnD started..."+ ( ( ((JTree)e.getSource()).getSelectionPath()).toString() ) );
}

public void mouseReleased(MouseEvent e)
{
JComponent c = (JComponent)e.getSource();
TransferHandler th2 = c.getTransferHandler();
th2.getPasteAction();
xpos = e.getX(); ypos = e.getY();
System.out.println("\n...DnD insert..."+ ( ((JTree)e.getSource() ).getClosestPathForLocation(xpos, ypos).toString() ) );
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

};
tree.addMouseListener(ml);

Now you find out with one click "what has been clicked".
My solution uses the items "name-string", not a referance to the object, but it is easy to change this.
Set your own cursor to simulate the look & feel of DnD.
The mouse listener tells you where the mouse was released (over which item). Since the tree is yours (that means you know all details and control the contents) you simply remove and add a tree item without even thinking about DnD-functionality - which was "faked" at least 50%.

I hate DnD in Java - I decided to do more faking whenever needed. I am sorry I have to admit that I don't understand fully why the "ordinary way" is not working but I want results. I hate to waste time. This is the shortest way to what you want.

Once you know better than that - please tell us how to do it. I guess I'm not the only one who cannot do it the way tutorials tell us.
 
I've been struggling with something similiar the last couple days. I am trying to create a dynamic jTree from data retrieved from a mysql query. The data is them placed in a two dimensional array. What I am trying to get working is a recursive loop that can identify parent nodes(around 10) from a data set of approximately 40 rows. Then it needs to identify the child nodes from the remaining 30 rows and place them with the correct parent. The two dimensional array is holding the object_id, object_name, and object_parent with the parent being one of the object_id's. Not sure if I explained it very well.

The problem I am having is that some of the root nodes have children which have their own children. I cannot get to jTree rendered correctly with this structure without hard coding the loops non-recursively, which is definately not the way this needs to work.

Any ideas?

Thanks...

lordsiris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top