Solved

How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?

Posted on 2008-10-30
6
383 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
  • 4
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
Comment Utility
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 59

Expert Comment

by:Kevin Cross
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jamie_lynn
Comment Utility
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 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
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 59

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 77
array11 challenge 16 50
parentbit challenge 3 50
Eclipse Neon and jdk 1.8.0 11 63
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now