Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Drawing at nodes
shalini gnana
Ranch Hand
Posts: 189
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to drawline against the parent node when its expanded and rectangle at all chil node....But its not showing any line...
How can i draw for each node?
Please help me..
/* * Reg.java * * Created on October 4, 2007, 3:12 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package javaapplication2; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Rectangle; import java.util.Hashtable; import javax.swing.JApplet; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreePath; /** * * @author 8563 */ public class reg extends JApplet { private JTree tree; private Drawing drawing; public DefaultMutableTreeNode createNodes() { DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode("RESOURCE"); DefaultMutableTreeNode resource1 = new DefaultMutableTreeNode("RESOURCE1"); DefaultMutableTreeNode resource2 = new DefaultMutableTreeNode("RESOURCE2"); DefaultMutableTreeNode resource3 = new DefaultMutableTreeNode("RESOURCE3"); rootnode.add(resource1); rootnode.add(resource2); rootnode.add(resource3); resource1.add(new DefaultMutableTreeNode("task1")); resource1.add(new DefaultMutableTreeNode("task2")); resource1.add(new DefaultMutableTreeNode("task3")); resource2.add(new DefaultMutableTreeNode("task1")); resource2.add(new DefaultMutableTreeNode("task2")); resource2.add(new DefaultMutableTreeNode("task3")); resource3.add(new DefaultMutableTreeNode("task1")); resource3.add(new DefaultMutableTreeNode("task2")); resource3.add(new DefaultMutableTreeNode("task3")); return rootnode; } public void init() { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { evt.printStackTrace(); } drawing = new Drawing(); tree = new JTree(createNodes()); tree.addTreeSelectionListener(drawing); JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(tree, BorderLayout.WEST); p.add(drawing, BorderLayout.CENTER); getContentPane().add(new JScrollPane(p)); } private class Drawing extends JPanel implements TreeSelectionListener { JTree tree = new JTree(); public String selectedParent = " "; private String selectedChildValue = ""; private int selectedChildY = -1; private Hashtable nodeStates = new Hashtable(); Object EXPANDED = new Object(); TreePath path; public void paintComponent (Graphics g) { super.paintComponent(g); int x = 30; for (int i=0; i<4; i++) { x += 70; g.drawString(""+i,x,25+i*30); g.drawRect(x,i*30,70,30) ; } if(isExpanded(path)== true) { g.drawString(" ",5,selectedChildY); } if (!selectedChildValue.equals("")) { g.drawString("Currently selected child: " + selectedChildValue + ".", 5, selectedChildY); } } public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getPath().getLastPathComponent(); boolean Path = e.isAddedPath(); System.out.println("node is : " + node); System.out.println("userobj : " + node.getUserObject()); if(e.isAddedPath() == true) { repaint(); } if (Path == true) { selectedChildValue = (String)node.getUserObject(); JTree tree = (JTree)e.getSource(); Rectangle bounds = tree.getPathBounds(e.getPath()); selectedChildY = bounds.y + bounds.height; } else { selectedChildValue = ""; selectedChildY = -1; } if(!node.isRoot()) { selectedParent = (String)node.getUserObject(); JTree tree = (JTree)e.getSource(); Rectangle bounds = tree.getPathBounds(e.getPath()); selectedChildY = bounds.y + bounds.x; } repaint(); } public boolean isExpanded( TreePath path) { if (path== null) return false; Object state = nodeStates.get(path); if ((state == null) || (state != EXPANDED)) return false; TreePath Path = path.getParentPath(); if(Path!= null) return isExpanded(Path); return true; } } }
Nathan Pruett
Bartender
Posts: 4121
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why start a new topic when this same post is already in
the original thread
? Closing this copy.
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JCheckBox in Jtree Problem
Is it possilbe to create Gantt chart without any tool
JTree
how to make the screen to move along with the JTree
How can I update multiple JTrees GUI Simutaneously??
More...