Solved

How do I make DefaultTreeSelectionModel isPathSelected() case insensitive?

Posted on 2008-10-30
6
386 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 59

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 59

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 59

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 59

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 70
mysql jsp example issue 32 75
program arguments vs VM arguments 4 31
Java array 21 87
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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.

730 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