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.
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 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 ]
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 ]
Then, make TreePaths of all these nodes to get their coordinates to draw the line/rectangle as you need to.
how to draw at each node
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).