Problems generating nodes in a JTree
Hi Folks,
I'm having difficulty in generating a JTree. The code follows. Basically, my application creates objects in a hierarchical fashion. The application creates objects of the classes Source, Requirement, DesignElement, ImplementationElement and TestSpecification.
Objects of each class have a parent relationship with the next, i.e., Source acts as parent to Requirement, Requirement to DesignElement etc. The Source, Requirement etc. classes each have a method for adding a child object, where the object is added to a Vector. Each parent may have more than one child.
The method generateTree() gets called first, which in turn calls addNodes(). addNodes() is supposed to add parent nodes and their children recursively. An example would be as follows;
root
|
--- S1 - Source 1
|
--- S2 - Source 2
| |
| --- R1 - Requirement 1
| |
| --- D1 - Design 1
|
--- S3 - Source 3
etc.
Providing that I only add Source and Requirement objects, the tree is generated correctly. If I try to add a Design Element or below,
either the node doesn't show, or I loss other nodes.
I've really been tearing my hair out over this and would really appreciate any advice over where I'm going wrong!
Martin :-(
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.Vector;
* Provides services for regenerating the tree when artifacts are added or removed.
public class TreeServices {
private static Vector sourceList, childArtifacts;
private static JTree tree;
private static DefaultMutableTreeNode newNode;
* Cycles through each source artifact, adding them as a node to the tree and calling
* addNodes() to add subsequent generations of children to the tree.
public static JTree generateTree(JTree tree, ArtifactDatabase database) {
sourceList = database.getSourceList();
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Project");
tree = new JTree(top);
DefaultMutableTreeNode node = null;
for (int i = 0; i < sourceList.size(); i++) {
node = new DefaultMutableTreeNode(sourceList.elementAt(i));
top.add(node);
addNodes((TraceabilityArtifact)sourceList.elementAt(i), node);
return tree;
* Adds children nodes to the tree recursively.
private static void addNodes(TraceabilityArtifact art, DefaultMutableTreeNode node) {
childArtifacts = art.getChildArtifacts();
if (!childArtifacts.isEmpty()) {
for (int i = 0; i < childArtifacts.size(); i++) {
newNode = new DefaultMutableTreeNode(childArtifacts.elementAt(i));
node.add(newNode);
if (!(((TraceabilityArtifact)childArtifacts.elementAt(i)).getChildArtifacts()).isEmpty()) {
addNodes((TraceabilityArtifact)childArtifacts.elementAt(i), newNode);
}
U need to call reload() method on ur TreeModel.
((DefaultTreeModel)tree.getModel()).reload()
i u wanna reload complete tree
or
((DefaultTreeModel)tree.getModel()).reload(TreeNode node) if u wanna reload a particular treenode
hope this works
Enjoy !!
Raj
Similar Messages
-
I have a problem with creating a node in the JTree
Hello All!
I have a problem when I try to create a new node in the JTree. If the parent node of the new node has any other children, I get following NullPointerException exception :
java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI.startEditing(BasicTreeUI.java:1959)
at javax.swing.plaf.basic.BasicTreeUI.startEditingAtPath(BasicTreeUI.java:506)
at javax.swing.JTree.startEditingAtPath(JTree.java:1921)
at ru.sirena2000.jxt.iface.action.TreeActionHandler.createNode(TreeActionHandler.java:62)
It seems to me, that path of creating node was not defined in the BasicTreeUI.
Note, if the parent node has no other children, the new node will be successfully created. How can I solve this problem? Help me, please!
I use java version 1.4.2-b28Sorry, I forgot to show a piece of code, where I try to create a new node. This piece of code see above. "tree.scrollPathToVisible(path);" is a string, which throws an exception.
tree.repaint();
TreePath path = tree.getSelectionPath();
if (path == null) {
return;
if (tree.isCollapsed(path)) {
tree.expandPath(path);
DefaultMutableTreeNode parentNode =
(DefaultMutableTreeNode)path.getLastPathComponent();
IconData childData = defineIconData(parentNode);
IconData newData = childData.copy();
newData.getNodeObject().clearNodeObj();
newData.getNodeObject().setNodeText("New");
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newData);
editingData = newData;
editingData.getNodeObject().setStatus(TreeNodeData.NEW_STATUS);
parentNode.add(newNode);
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
path = path.pathByAddingChild(newNode);
tree.scrollPathToVisible(path);
tree.startEditingAtPath(path);
saveChangesInBuffer(newNode, TreeNodeData.NEW_STATUS);
model.nodeStructureChanged(parentNode); -
Getting TreePath for all nodes in a JTree
Hi everybody
I want to get TreePath for all the nodes in a JTree
Can anybody help me?Hi,
i tried to used your code.
there is a problem, it is not taking the entire paths.
so if you have some children,it takes the paths only for the fathers.
so i correct it to the following way(it will take the entire paths):
public void getPathForAllNodes(TreePath path)
Object node = path.getLastPathComponent();
pathArrayList.add(path);
TreeModel model = tree.getModel();
if(model.isLeaf(node))
return;
int num = model.getChildCount(node);
for(int i = 0; i < num; i++)
//pathArrayList.add(path);
getPathForAllNodes(path.pathByAddingChild(model.getChild(node, i)));
}thanks for your code -
How to remove all nodes (except root node)from a Jtree?
How to remove all nodes (except the root node)from a Jtree?
Either:
- remove all children of root.
- save the root node, throws away the tree model, build a new TreeModel with the saved root, set the new TreeModel in the JTree.
- implement your own TreeModel, which would support an emptyExceptRoot() method.
IMHO, using the DefautlTreeModel and DefaultMutableTreeNode does lead to all sorts of small problems when the app evolves, and implementing your own TreeNode and TreeModel is not that hard and much more efficient. -
Can I Have JTable As Leaf Node Of A JTree?
Hello there,
I want to show jtable on a click of a node of a tree. I have seen Treetable utlility on SUN's site. But it not quite what i want.
Does any one have any idea how to show JTable as a leaf node of a JTree.
E.g There is a tree called Components. viz.
Component
|Node1
|Node2
On click of Node1 i should see Table1 and on click of Node2 i should see Table2.
If anyone has any idea, can you explain how to do it Or some site name where help on this problem is available?
Please mail back.
Regards,
AmitI used insertNodeInto() to inser a new node into the tree, and it can be displayed. But when I used addTreeSelectionListener() to click on the newly added node, it cann not reaspond the click. Following is my original addTreeSelectionListener(). You see, if you click a node of FARInfo, then it will open a form, and after user filled in and submit it, a new node will be added into the Jtree. If you click a node of FilledInfo, then the user should view the content of this new node. But now, it seems that the sencond click cannot work. Thanks for your help.
tree.addTreeSelectionListener( new TreeSelectionListener()
public void valueChanged(TreeSelectionEvent e4)
DefaultMutableTreeNode node = ( DefaultMutableTreeNode )
(tree.getLastSelectedPathComponent ());
Object nodeInfo = node.getUserObject();
if (node.isLeaf())
if ( nodeInfo instanceof FARInfo )
FARInfo category = (FARInfo) nodeInfo;
displayURL ( category.categoryURL );
displayForm ( category.farFormName );
if ( DEBUG )
System.out.print ( category.categoryURL + ":\n" );
else if ( nodeInfo instanceof FilledInfo )
FilledInfo category2 = ( FilledInfo ) nodeInfo;
displayFilledForm ( category2.num );
}else
return; -
How do i expand all the nodes in a jtree
Hi,
I am working on a project where i need to expand all the nodes of a jtree i have tried a few different ways but it never seems to expand all the nodes..
I would be very greatful if someone could point me in the right direction
cheers
Maryyou could use the following method that expands nodes recursively
expandNode( myTree, myRootNode, new TreePath( myRootNode ) );
public static void expandNode( JTree tree, TreeNode node, TreePath path ) {
tree.expandPath( path );
int i = node.getChildCount( );
for ( int j = 0; j< i; j++ ) {
TreeNode child = node.getChildAt( j );
expandNode( tree, child , path.pathByAddingChild( child ) );
} -
How to get total number of nodes in a JTree?
Hi,
I am trying to get total number of nodes in a JTree, and cannot find a way to do it.
The current getRowCount() method returns the number of rows that are currently being displayed.
Is there a way to do this or I am missing something?
thanks,How many nodes does this tree have?
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.*;
public class BigTree {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
TreeModel model = new TreeModel() {
private String node = "Node!";
@Override
public void valueForPathChanged(TreePath path,
Object newValue) {
// not mutable
@Override
public void removeTreeModelListener(TreeModelListener l) {
// not mutable
@Override
public boolean isLeaf(Object node) {
return false;
@Override
public Object getRoot() {
return node;
@Override
public int getIndexOfChild(Object parent, Object child) {
return child == node ? 0 : -1;
@Override
public int getChildCount(Object parent) {
return 1;
@Override
public Object getChild(Object parent, int index) {
return node;
@Override
public void addTreeModelListener(TreeModelListener l) {
// not mutable
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(new JTree(model)));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}But for bounded tree model using DefaultMutableTreeNode look at bread/depth/preorder enumeration methods to walk the entire tree. Or look at the source code for those and adapt them to work with the TreeModel interface. -
Problem generating stubs for Java EJB web service deployed in OAS
I created an EJB web service and I've successfully deployed it in my Oracle App Server. Some of the methods work fine but others produce the ff error:
org.apache.soap.SOAPException - java.lang.IllegalArgumentException: No Serializer found to serialize [classname] using encoding style [encoding]It seems that the objects specified as parameters in the web service methods exposed are the only ones that had stubs generated for them. Other objects I use, which are usually wrapped inside a Vector, did not have generated stubs.
Example:
public String loginUser(UserDTO userDTO) throws RemoteException, NamingException, SQLException;
public String addItems (Vector vecItems) throws RemoteException, NamingException, SQLException; // where vecItems is a collection of ItemDTO objects In this scenario, stubs were generated for the UserDTO class, but not for the ItemDTO class. In effect, calling the addItems method resulted to the exception I mentioned above.
I did a workaround wherein I declared a dummy method which accepted all the types of objects I needed as parameters so all the necessary stubs can be generated, but this fix doesn't feel like it's the proper solution to my problem.
If anyone can help me, it would be greatly appreciated. Thanks!Crossposted:
Problem generating stubs for Java EJB web service deployed in OAS -
Problem in NODE 1 after reboot
Hi,
Oracle Version:11gR2
Operating System:Cent Os
Hi we have some problem in node 1 after sudden reboot of both the nodes when the servers are up the database in node 2 started automatically but in node 1 we started manually.
But inthe CRSCTL command it is showing that node 1 database is down as show below.
[root@rac1 bin]# ./crsctl stat res -t
NAME TARGET STATE SERVER STATE_DETAILS
Local Resources
ora.ASM_DATA.dg
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.ASM_FRA.dg
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.LISTENER.lsnr
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.OCR_VOTE.dg
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.asm
ONLINE ONLINE rac1 Started
ONLINE ONLINE rac2 Started
ora.eons
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.gsd
OFFLINE OFFLINE rac1
OFFLINE OFFLINE rac2
ora.net1.network
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.ons
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.registry.acfs
ONLINE ONLINE rac1
ONLINE ONLINE rac2
Cluster Resources
ora.LISTENER_SCAN1.lsnr
1 ONLINE ONLINE rac2
ora.LISTENER_SCAN2.lsnr
1 ONLINE ONLINE rac1
ora.LISTENER_SCAN3.lsnr
1 ONLINE ONLINE rac1
ora.oc4j
1 OFFLINE OFFLINE
ora.qfundrac.db
1 OFFLINE OFFLINE
2 ONLINE ONLINE rac2 Open
ora.rac1.vip
1 ONLINE ONLINE rac1
ora.rac2.vip
1 ONLINE ONLINE rac2
ora.scan1.vip
1 ONLINE ONLINE rac2
ora.scan2.vip
1 ONLINE ONLINE rac1
ora.scan3.vip
1 ONLINE ONLINE rac1but for the below command it is showing both the nodes are up
SQL> select inst_id,status,instance_role,active_state from gv$instance;
INST_ID STATUS INSTANCE_ROLE ACTIVE_ST
1 OPEN PRIMARY_INSTANCE NORMAL
2 OPEN PRIMARY_INSTANCE NORMALhere is the output for cluvfy .
[grid@rac1 bin]$ ./cluvfy stage -post crsinst -n rac1,rac2 -verbose
Performing post-checks for cluster services setup
Checking node reachability...
Check: Node reachability from node "rac1"
Destination Node Reachable?
rac2 yes
rac1 yes
Result: Node reachability check passed from node "rac1"
Checking user equivalence...
Check: User equivalence for user "grid"
Node Name Comment
rac2 passed
rac1 passed
Result: User equivalence check passed for user "grid"
Checking time zone consistency...
Time zone consistency check passed.
Checking Cluster manager integrity...
Checking CSS daemon...
Node Name Status
rac2 running
rac1 running
Oracle Cluster Synchronization Services appear to be online.
Cluster manager integrity check passed
UDev attributes check for OCR locations started...
Result: UDev attributes check passed for OCR locations
UDev attributes check for Voting Disk locations started...
Result: UDev attributes check passed for Voting Disk locations
Check default user file creation mask
Node Name Available Required Comment
rac2 0022 0022 passed
rac1 0022 0022 passed
Result: Default user file creation mask check passed
Checking cluster integrity...
Node Name
rac1
rac2
Cluster integrity check passed
Checking OCR integrity...
Checking the absence of a non-clustered configuration...
All nodes free of non-clustered, local-only configurations
ASM Running check passed. ASM is running on all cluster nodes
Checking OCR config file "/etc/oracle/ocr.loc"...
OCR config file "/etc/oracle/ocr.loc" check successful
Disk group for ocr location "+OCR_VOTE" available on all the nodes
Checking size of the OCR location "+OCR_VOTE" ...
Size check for OCR location "+OCR_VOTE" successful...
Size check for OCR location "+OCR_VOTE" successful...
WARNING:
This check does not verify the integrity of the OCR contents. Execute 'ocrcheck' as a privileged user to verify the contents of OCR.
OCR integrity check passed
Checking CRS integrity...
The Oracle clusterware is healthy on node "rac2"
The Oracle clusterware is healthy on node "rac1"
CRS integrity check passed
Checking node application existence...
Checking existence of VIP node application
Node Name Required Status Comment
rac2 yes online passed
rac1 yes online passed
Result: Check passed.
Checking existence of ONS node application
Node Name Required Status Comment
rac2 no online passed
rac1 no online passed
Result: Check passed.
Checking existence of GSD node application
Node Name Required Status Comment
rac2 no does not exist ignored
rac1 no does not exist ignored
Result: Check ignored.
Checking existence of EONS node application
Node Name Required Status Comment
rac2 no online passed
rac1 no online passed
Result: Check passed.
Checking existence of NETWORK node application
Node Name Required Status Comment
rac2 no online passed
rac1 no online passed
Result: Check passed.
Checking Single Client Access Name (SCAN)...
SCAN VIP name Node Running? ListenerName Port Running?
qfund-rac.qfund.net rac2 true LISTENER 1521 true
Checking name resolution setup for "qfund-rac.qfund.net"...
SCAN Name IP Address Status Comment
qfund-rac.qfund.net 192.168.8.118 passed
qfund-rac.qfund.net 192.168.8.119 passed
qfund-rac.qfund.net 192.168.8.117 passed
Verification of SCAN VIP and Listener setup passed
OCR detected on ASM. Running ACFS Integrity checks...
Starting check to see if ASM is running on all cluster nodes...
ASM Running check passed. ASM is running on all cluster nodes
Starting Disk Groups check to see if at least one Disk Group configured...
Disk Group Check passed. At least one Disk Group configured
Task ACFS Integrity check passed
Checking Oracle Cluster Voting Disk configuration...
Oracle Cluster Voting Disk configuration check passed
Checking to make sure user "grid" is not in "root" group
Node Name Status Comment
rac2 does not exist passed
rac1 does not exist passed
Result: User "grid" is not part of "root" group. Check passed
Checking if Clusterware is installed on all nodes...
Check of Clusterware install passed
Checking if CTSS Resource is running on all nodes...
Check: CTSS Resource running on all nodes
Node Name Status
rac2 passed
rac1 passed
Result: CTSS resource check passed
Querying CTSS for time offset on all nodes...
Result: Query of CTSS for time offset passed
Check CTSS state started...
Check: CTSS state
Node Name State
rac2 Observer
rac1 Observer
CTSS is in Observer state. Switching over to clock synchronization checks using NTP
Starting Clock synchronization checks using Network Time Protocol(NTP)...
NTP Configuration file check started...
The NTP configuration file "/etc/ntp.conf" is available on all nodes
NTP Configuration file check passed
Checking daemon liveness...
Check: Liveness for "ntpd"
Node Name Running?
rac2 yes
rac1 yes
Result: Liveness check passed for "ntpd"
Checking NTP daemon command line for slewing option "-x"
Check: NTP daemon command line
Node Name Slewing Option Set?
rac2 yes
rac1 yes
Result:
NTP daemon slewing option check passed
Checking NTP daemon's boot time configuration, in file "/etc/sysconfig/ntpd", for slewing option "-x"
Check: NTP daemon's boot time configuration
Node Name Slewing Option Set?
rac2 yes
rac1 yes
Result:
NTP daemon's boot time configuration check for slewing option passed
NTP common Time Server Check started...
NTP Time Server ".INIT." is common to all nodes on which the NTP daemon is running
NTP Time Server ".LOCL." is common to all nodes on which the NTP daemon is running
Check of common NTP Time Server passed
Clock time offset check from NTP Time Server started...
Checking on nodes "[rac2, rac1]"...
Check: Clock time offset from NTP Time Server
Time Server: .INIT.
Time Offset Limit: 1000.0 msecs
Node Name Time Offset Status
rac2 0.0 passed
rac1 0.0 passed
Time Server ".INIT." has time offsets that are within permissible limits for nodes "[rac2, rac1]".
Time Server: .LOCL.
Time Offset Limit: 1000.0 msecs
Node Name Time Offset Status
rac2 -29.328 passed
rac1 -84.385 passed
Time Server ".LOCL." has time offsets that are within permissible limits for nodes "[rac2, rac1]".
Clock time offset check passed
Result: Clock synchronization check using Network Time Protocol(NTP) passed
Oracle Cluster Time Synchronization Services check passed
Post-check for cluster services setup was successful.
[grid@rac1 bin]$Please help me how to solve this problem.
Thanks & regards
Poorna Prasad.SHi All,
Now again i reboothed the database again manually and the database is no up on both the node.
Here is the output for few commands
[grid@rac1 bin]$ ./crs_stat -t
Name Type Target State Host
ora....DATA.dg ora....up.type OFFLINE OFFLINE
ora.ASM_FRA.dg ora....up.type OFFLINE OFFLINE
ora....ER.lsnr ora....er.type ONLINE ONLINE rac1
ora....N1.lsnr ora....er.type ONLINE ONLINE rac1
ora....N2.lsnr ora....er.type ONLINE ONLINE rac2
ora....N3.lsnr ora....er.type ONLINE ONLINE rac2
ora....VOTE.dg ora....up.type ONLINE ONLINE rac1
ora.asm ora.asm.type ONLINE ONLINE rac1
ora.eons ora.eons.type ONLINE ONLINE rac1
ora.gsd ora.gsd.type OFFLINE OFFLINE
ora....network ora....rk.type ONLINE ONLINE rac1
ora.oc4j ora.oc4j.type OFFLINE OFFLINE
ora.ons ora.ons.type ONLINE ONLINE rac1
ora....drac.db ora....se.type OFFLINE OFFLINE
ora....SM1.asm application ONLINE ONLINE rac1
ora....C1.lsnr application ONLINE ONLINE rac1
ora.rac1.gsd application OFFLINE OFFLINE
ora.rac1.ons application ONLINE ONLINE rac1
ora.rac1.vip ora....t1.type ONLINE ONLINE rac1
ora....SM2.asm application ONLINE ONLINE rac2
ora....C2.lsnr application ONLINE ONLINE rac2
ora.rac2.gsd application OFFLINE OFFLINE
ora.rac2.ons application ONLINE ONLINE rac2
ora.rac2.vip ora....t1.type ONLINE ONLINE rac2
ora....ry.acfs ora....fs.type ONLINE ONLINE rac1
ora.scan1.vip ora....ip.type ONLINE ONLINE rac1
ora.scan2.vip ora....ip.type ONLINE ONLINE rac2
ora.scan3.vip ora....ip.type ONLINE ONLINE rac2
[grid@rac1 bin]$ srvctl status nodeapps -n rac1,rac2
-bash: srvctl: command not found
[grid@rac1 bin]$ ./srvctl status nodeapps -n rac1,rac2
PRKO-2003 : Invalid command line option value: rac1,rac2
[grid@rac1 bin]$ ./srvctl status nodeapps -n rac1
-n <node_name> option has been deprecated.
VIP rac1-vip is enabled
VIP rac1-vip is running on node: rac1
Network is enabled
Network is running on node: rac1
GSD is disabled
GSD is not running on node: rac1
ONS is enabled
ONS daemon is running on node: rac1
eONS is enabled
eONS daemon is running on node: rac1
[grid@rac1 bin]$ ./srvctl status nodeapps -n rac2
-n <node_name> option has been deprecated.
VIP rac2-vip is enabled
VIP rac2-vip is running on node: rac2
Network is enabled
Network is running on node: rac2
GSD is disabled
GSD is not running on node: rac2
ONS is enabled
ONS daemon is running on node: rac2
eONS is enabled
eONS daemon is running on node: rac2Here is the output for crsctl stat res -t
[grid@rac1 bin]$ ./crsctl stat res -t
NAME TARGET STATE SERVER STATE_DETAILS
Local Resources
ora.ASM_DATA.dg
OFFLINE OFFLINE rac1
OFFLINE OFFLINE rac2
ora.ASM_FRA.dg
OFFLINE OFFLINE rac1
OFFLINE OFFLINE rac2
ora.LISTENER.lsnr
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.OCR_VOTE.dg
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.asm
ONLINE ONLINE rac1 Started
ONLINE ONLINE rac2 Started
ora.eons
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.gsd
OFFLINE OFFLINE rac1
OFFLINE OFFLINE rac2
ora.net1.network
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.ons
ONLINE ONLINE rac1
ONLINE ONLINE rac2
ora.registry.acfs
ONLINE ONLINE rac1
ONLINE ONLINE rac2
Cluster Resources
ora.LISTENER_SCAN1.lsnr
1 ONLINE ONLINE rac1
ora.LISTENER_SCAN2.lsnr
1 ONLINE ONLINE rac2
ora.LISTENER_SCAN3.lsnr
1 ONLINE ONLINE rac2
ora.oc4j
1 OFFLINE OFFLINE
ora.qfundrac.db
1 OFFLINE OFFLINE
2 OFFLINE OFFLINE
ora.rac1.vip
1 ONLINE ONLINE rac1
ora.rac2.vip
1 ONLINE ONLINE rac2
ora.scan1.vip
1 ONLINE ONLINE rac1
ora.scan2.vip
1 ONLINE ONLINE rac2
ora.scan3.vip
1 ONLINE ONLINE rac2What is going wrong here .
Thanks & Regards,
Poorna Prasad.S
Edited by: SIDDABATHUNI on Apr 30, 2011 2:06 PM
Edited by: SIDDABATHUNI on Apr 30, 2011 2:10 PM -
How to use custom nodes in a JTree without reinventing the wheel?
Hello,
Each node contains two JTextAreas in a Box layout and a few JButtons.
I wish to display these nodes in a JTree.
Reading the tutorial, it seems I would have to reimplement the TreeModel, TreeCellRenderer/Editor, MutableTreeNode interfaces etc.
Can I use the DefaultTreeModel, and other standard widgets (great stuff!) to minimize the amount of reimplementation I must do. aka avoid reinventing the wheel? I was thinking of extending my node from the DefaultMutableTreeNode class - however it does not display the nodes with the TextAreas, buttons etc.
any help appreciated.
thanks,
Anilwas able to fix it over here:
http://forum.java.sun.com/thread.jspa?messageID=4089777 -
Can the name of a specific node in a JTree be returned by MouseEvent?
I have created a mouseListener for a JTree. When a node in the JTree is clicked it should display the name of the node in a JOptionPane. Currently, I have the following:
tree.addMouseListener(new MouseAdapter()
public void mouseClicked(MouseEvent me)
if(me.getClickCount() % 2 == 0)
String s = "Blank";
s = (me.getSource()).toString();
JOptionPane.showMessageDialog(null, "Double clicked "+ s, "two", JOptionPane.PLAIN_MESSAGE);
}//mouseClicked
});//MouseListener
This gives me the class X Y value, border, maxsize, etc.... when a node is double clicked.
Does anyone know of a better way?Don't use MouseListener.
Instead, make yourself a TreeSelectionListener as follows:
public class WhererverYourTreeIs implements TreeSelectionListener
public void valueChanged(TreeSelectionEvent e)
TreePath path = e.getPath();
System.out.println(path.getLastPathComponent());
public void initStuff()
tree.addTreeSelectionLIstener(this);
} -
Can anyone tell me how to go about sorting the nodes in a JTree?
The best way to do it is thru the treemodel. Either adjust the order in a DefaultTreeModel, or create your own TreeModel.
-
How do I get a dotted line to connect nodes in a JTree?
I am trying to recreate a Windows Explorer application, does anyone know how they get the dotted lines to connect the nodes in the JTree????
JTree uses a specific line style to represent the edges between nodes. The default is no edges, but we can set JTree�s lineStyle client property so that each parent node appears connected to each of its child nodes by an angled line:
myJTree.putClientProperty("JTree.lineStyle", "Angled");
We can also set this property such that each tree cell is separated by a horizontal line:
myJTree.putClientProperty("JTree.lineStyle", "Horizontal");
To disable the line style:
myJTree.putClientProperty("JTree.lineStyle", "None");
As with any Swing component, we can also change the UI resource defaults used for all instances of the JTree class. For instance, to change the color of the lines used for rendering the edges between nodes as described above, we can modify the entry in the UI defaults table for this resource as follows:
UIManager.put("Tree.hash", new ColorUIResource(Color.lightGray))
Hope this serves your purpose.
Regards,
Sachin Shanbhag -
How can I get a count of ALL nodes in a JTree?
Not sure if I am missing something here or what. Is there an easy way to determine the total number of nodes in a JTree? I thought there would be a method to return this, but I'm not seeing it in JTree or DefaultTreeModel. Do I have to start at the root and recursively get child counts? Yuck!
JamieYou are absolutely right! Create a recursive method and count all your children from the root.
Denis Krukovsky
http://dotuseful.sourceforge.net/ -
Dynamically changing the color of nodes in a JTree
I have created a JTree and want to dynamically change the color of some of the TreeNodes as my program runs. The JTree is displayed in a JPanel. There is an algorithm built into which identifies the nodes whose color I need to change. No user actions is performed (everythign is internal to the program).
It seems that in the TreeCellRender only kicks in when the tree is first displayed. How do I get it to re-render the tree when the TreeModel changes? It seems that the Listeners are only looking for external user interactions.
Any help would be greatly appreciated.
Thanks!I think I was a bit too vague in my question. Let me try again...
I have changed an attribute in a node in a JTree. This attribute is changed after the tree was initially rendered, but while the program is still running. I want to tell the TreeCellRenderer to look again at this node since that attribute that was changed will effect how the node should be renderered. I tried using the nodeChanged() method, but it did not work (the colot of the node did not change). Any advise how I can do this?
Thanks!
Maybe you are looking for
-
Google news buttons no longer working
I'm still having some problems with Google News that I asked about in an earlier question (https://support.mozilla.org/en-US/questions/947273) In Firefox 18 and 19, the buttons to expand / contract the news items on http://news.google.com/ are no lon
-
IWS 6.0 100% CPU utilization hanging- very urgent
Hi, We are using Iplanet Web server 6.0 on windows-2000 SP2.The problem we are facing is after 10 concurrent users have logged in the CPU utilization shoots up to 100% and we have to reboot the systesm Our billing Application is affected very much du
-
Hi gurus, I have entered two items in document type MV with two item categories MVN and TAO. while doing billing this rental contract, For the first item(MVN), system picks the same pricing procedure as of contract(PSER01) and whereas f
-
MIDI over direct USB connection between two computers?
I'm trying to find out if it's possible to send MIDI between two computers using a direct USB to USB connection (no MIDI interface). I have not seen this done before, I don't think, but I don't know why it shouldn't be possible. Thanks for any tips o
-
Hi Having acquired (2nd hand) a Toshiba Equium L10-273 with broken screen, which I subsequently replaced and then upgraded the memory, I would like to reload windows xp home from scratch but unfortunately the disc was misplaced before I acquired the