?
Solved

How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?

Posted on 2008-10-30
6
Medium Priority
?
390 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
Industry Leaders: 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!

 

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 1500 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

765 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