Solved

How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?

Posted on 2008-10-30
6
387 Views
Last Modified: 2008-11-17
Hi,
How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?
Currently it is a case sensitive match.

How do I match the path of the Tree case not sensitive?

Thanks
Jamie
0
Comment
Question by:jamie_lynn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22847046
Not sure I follow as isPathSelected() method takes a TreePath object as parameter.  This is an object equality issue.  The objects must equal -- this is not a string match.  Maybe I am missing something though.

Are you referring to this class:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/tree/DefaultTreeSelectionModel.html
0
 

Author Comment

by:jamie_lynn
ID: 22847267
Yes, it is the correct class.
In DefaultTreeCellRendererl.getTreeCellRendererComponent(JTree tree, ....)
I call
TreePath path = tree.getPathForRow(row);
then call
selectionModel.isPathSelected(path);

If I call
((javax.swing.tree.DefaultMutableTreeNode)path.getPath()[0]).getUserObject()
It will return be a String
(java.lang.String) root

Thanks
Jamie
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22847286
The isPathSelected will tell you true or false if the TreePath you pass it is the currently selected one.
boolean pathSelected = selectionModel.isPathSelected(path);
if (pathSelected) {
   // do something
}

If your intent is to set the TreePath as selected path, then you must use:
selectionModel.setSelectedPath(path);

Hope that helps.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:jamie_lynn
ID: 22847375
I understand that part. My question was that if i call
boolean pathSelected = selectionModel.isPathSelected(path);

pathSelected is true even though when I call
selectionModel.setSelectedPath(newpath);
where newpath is [root, top]
and path is  [root, TOP]

The difference in case makes the pathselected false
Is there a way to make it case insensitive?

Thanks
Jamie
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22847505
There is probably an easy way, but I don't know what it is sorry.

This is the crazy idea rattling around in my head, but not sure if you can even use customized TreePath objects, especially for your particular situation but this is what I am thinking to make the path comparison case insensitive.

Regards,
Kevin
private class MyTreePath extends TreePath {
		private static final long serialVersionUID = 1174436545852519074L;
 
		@Override
		public boolean equals(Object o) {
			if (super.equals(o)) return true;
			
			if (o instanceof TreePath) {
				Object[] oPath = ((TreePath)o).getPath();
				Object[] iPath = this.getPath();
				
				if (oPath.length == iPath.length) {
					for (int i=0; i < iPath.length; i++) {
						if (!iPath[i].toString().equalsIgnoreCase(oPath.toString())) return false;
					}
					return true;
				}
			}
			return false;
		}
		
	}

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22847510
Had it as private inner class, so you can remove the private modifier on the class definition.  Left over code.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question