Originally posted by shalini gnana: I want to place both the tree and my drawing with in the same panel..I used borderlayout..But its getting overlap..How can i do it..Help me..
If your drawing is an image you can override the paintComponent of the panel and draw the image as the background. For this to be visible you will have to make all the components (tree, viewport, scrollpane etc) which go into this panel, transperant.
Then you have to calculate the width of the tree, and only start painting at that border. Instead of starting at x = 0, you start at x = tree.getX() + tree.getWidth().
I still don't understand why this has to be done all in the same panel...
it would be a lot simpler to just make the component that is painting one component, add it to the BorderLayout.CENTER of a panel, add the tree to the BorderLayout.WEST/EAST whatever, and then add this panel to a JScrollpane. Then they would both scroll together, and you wouldn't have to do all this wacky stuff to try to get around the layout managers.
Alternatively, if you want the tree and drawing to scroll separately, add the tree to a scrollpane, add the drawing to another scrollpane, and add the scrollpane containing the drawing to BorderLayout.CENTER of the panel, and add the scrollpane containing the tree to to BorderLayout.WEST/EAST.
I drawn inside the paintComponent method..How can i add this to the east of my borderlayout? I want to add both tree and drawing to the same panel,because when i expand my tree the drawing against to the child should show...And my requirement is so...
[ October 25, 2007: Message edited by: shalini gnana ]
I drawn inside the paintComponent method..How can i add this to the east of my borderlayout?
As above - separate this into parent and child panels - have the child panel provide the drawing (paintComponent), and have the parent panel provide the layout.
I want to add both tree and drawing to the same panel,because when i expand my tree the drawing against to the child should show...
That would normally be done using TreeSelectionListener - you could make your drawing component implement TreeSelectionListener, add it as a listener to the tree, and react to the tree expansion events by determining what child node needs to be repainted and repainting the drawing component.
Its throwing NullPointException at: rightPanel.add(tree,BorderLayout.WEST);
I don't see the "tree" variable being initialized anywhere in the code provided.
Here's an example of how to pull this all together -
(Edit - changed TreeExpansionListener to TreeSelectionListener in explanation. Posted example code.) [ October 25, 2007: Message edited by: Nathan Pruett ]
Thank you..Now my tree is displaying inside the panel..But,now the issue is,i added scrollbar to my panel..But its not showing..What might be the reason?
Thanks alot for your help.. Infact that is not my reruirement... I want to produce a gantt chart for task scheduling,my requirement is...the task 'll be in the tree and the dates 'll be given in top of the panel as single row..I'll draw filled rectangle against the task and date..Now the problem is if the task is selected the all the rectangles should show or it should not...How to achieve it? Is it possible to do
[ October 26, 2007: Message edited by: shalini gnana ]
I've changed my previous example slightly - I've added a JScrollPane at the top level, and also taken the y location of the selected child into account in the "Drawing" component.
Thanks alot..Instead of drawstring i used drawrect,But,now the problem is the drawing have to be repainted(i.e., it should not show) only when the node gets collapsed...Is their any method to do so? Please help me?
In the public void valueChanged(TreeSelectionEvent e) method of TreeSelectionListener, you can call e.isAddedPath() to determine if the event is a selection or deselection event. You can get the parent node from the TreePath using path.getParentPath().
It sounds like you need to do a lot more processing on this than just the location of the original child. Instead of just the Y location of the selected child, you'll need to use the TreePath of the currently selcted child to get:
that child's parent node;
all the children nodes of that parent.
Then, make TreePaths of all these nodes to get their coordinates to draw the line/rectangle as you need to.
If you want to draw *in* the tree node, you'd need to create a TreeCellRenderer.
If you want to draw *in* the DrawingPanel at the location of a node, you'll need to get the TreePath to that node, call tree.getPathBounds(path) to get the bounding rectangle of that path - that will get you coordinates for that node you can use to draw in the DrawingPanel. If you have a TreePath of one node, you can get the TreePath of it's parent node by calling path.getParentPath(). If you're using a DefaultTreeModel, you can call path.getLastPathComponent() and cast the result to a TreeNode. You can then call node.children() to get an Enumeration of all the children. You can create a new TreePath to each child by using the parent TreePath and creating a new TreePath with the parent path and the child node - TreePath childPath = new TreePath(parentPath, childNode).
Post by:autobot
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking