• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to hide the tree handles, URGENT! Thanks!  RSS feed

 
Linda Xu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JTree on the GUI, for some reason I want to hide all the handles(which is shown at the left of a node when it have child).
In the Java API, there is a method called
setShowRootHandles(false)
But this will only hide the handles at the topmost level, how about the lower levels? I want to hide them also.
Thanks!
 
Luong Nguyen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Xu,
You can create a TreeUI, after you use method JTree.setUI(your TreeUI). You can overwrite two methods installKeyboardActions and isToggleEvent to hide handles. Please refer to BasicTreeUI.
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.*;
public class MyTreeUI extends BasicTreeUI {
public class MyMouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent me) {
if (tree != null && tree.isEnabled()) {
tree.requestFocus();
TreePath path = getClosestPathForLocation(tree, me.getX(), me.getY());
if (path != null) {
Rectangle bounds = getPathBounds(tree, path);
if (me.getY() > (bounds.y + bounds.height)) {
return;
}
// Preferably checkForClickInExpandControl could take
// the Event to do this it self!
if (SwingUtilities.isLeftMouseButton(me))
checkForClickInExpandControl(path, me.getX(), me.getY());
int x = me.getX();
// Perhaps they clicked the cell itself. If so,
// select it.
if (x > bounds.x) {
if (x <= (bounds.x + bounds.width)) {
selectPathForEvent(path, me);
}
}
}
}
}
}
public static ComponentUI createUI(JComponent c) {
return new MyTreeUI();
}
public MyTreeUI() {
super();
}
protected MouseListener createMouseListener() {
return new MyMouseHandler();
}
protected void installKeyboardActions() {
}
protected boolean isToggleEvent(MouseEvent me) {
return false;
}
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!