*
The moose likes Swing / AWT / SWT and the fly likes Tree and drawing within the same panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Tree and drawing within the same panel" Watch "Tree and drawing within the same panel" New topic
Author

Tree and drawing within the same panel

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
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..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I want my tree at the left side of my panel and drawing at the centre without overlaping each other..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

How do you create the drawing? Have you overridden the paint or paintComponent from the same panel that you are adding the tree to?

If so, try putting that drawing code in a different panel, and add that panel to the first panel using BorderLayout.CENTER.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I don't want to do it in that way because i want to add scrollBars to it, that either the drawing area and the tree scroll simultaneously..


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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().
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
No i'm not getting you..Where should i have to perform this calculation?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

In the code where you paint your drawing.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.



-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
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 ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Any Help?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Its throwing NullPointException at: rightPanel.add(tree,BorderLayout.WEST);





Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121


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 ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
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?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
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 ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I want to draw against particular node..How can i do that?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Any help..please..
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I want to draw the rectangle against the node which is selected...
Can anyone,help me...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sounds like a "TreeTable" - there's an example of how to create a TreeTable yourself on Sun Developer Network. The JDesktop project also provides a JTreeTable implementation.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
No,I don't want to do it in that way..I want to draw in my drawing area..Please,help me...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
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?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Anyhelp,please
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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().
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
THANKS ALOT...I'll try it out....
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I want to drawline against the parent node when its expanded....But its not showing any line...
How can i draw for each node?
Please help me..

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Please help me..
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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.
    shalini gnana
    Ranch Hand

    Joined: Sep 18, 2007
    Posts: 189
    I'm not getting what you are trying to say...
    Can you explain me,please..
    shalini gnana
    Ranch Hand

    Joined: Sep 18, 2007
    Posts: 189

    tree.putClientProperty("JTree().lineStyle","Horizontal");


    I tried the above thing in my code but i'm not getting it?Help me...

    Nathan Pruett
    Bartender

    Joined: Oct 18, 2000
    Posts: 4121

    It's tree.putClientProperty("JTree.lineStyle", "Horizontal"); (no parentheses after the "JTree" property)

    And this changes how the lines that connect nodes are drawn within the tree - I thought that you wanted to draw lines on the "Drawing" panel?
    shalini gnana
    Ranch Hand

    Joined: Sep 18, 2007
    Posts: 189
    Yes,i need to do that too...Can you explain me little bit elobrately how to draw at each node?
    Please help me...
    shalini gnana
    Ranch Hand

    Joined: Sep 18, 2007
    Posts: 189
    Is there any way i can achieve it?
    Nathan Pruett
    Bartender

    Joined: Oct 18, 2000
    Posts: 4121


    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).
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Tree and drawing within the same panel