Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to hide the tree handles, URGENT! Thanks!

Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
But this will only hide the handles at the topmost level, how about the lower levels? I want to hide them also.
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()) {
TreePath path = getClosestPathForLocation(tree, me.getX(), me.getY());
if (path != null) {
Rectangle bounds = getPathBounds(tree, path);
if (me.getY() > (bounds.y + bounds.height)) {
// 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() {
protected MouseListener createMouseListener() {
return new MyMouseHandler();
protected void installKeyboardActions() {
protected boolean isToggleEvent(MouseEvent me) {
return false;
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic