File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Swing / AWT / SWT
Author
Drawing at nodes
shalini gnana
Ranch Hand
Joined: Sep 18, 2007
Posts: 189
posted
Nov 05, 2007 06:02:00
0
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
Joined: Oct 18, 2000
Posts: 4121
I like...
posted
Nov 05, 2007 07:40:00
0
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.
I agree. Here's the link:
http://aspose.com/file-tools
subject: Drawing at nodes
Similar Threads
JCheckBox in Jtree Problem
Tree and drawing within the same panel
how to make the screen to move along with the JTree
JTree
Is it possilbe to create Gantt chart without any tool
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter