Search code examples
javaswingjtree

Java: JTree with plus/minus icons for expansion and collapse?


How do I make my Jtree look like something below, with plus and minus icons that allows expansion and collapse?

Currently, the default JTree only expands and collapses when you double click. I want to override this double click for another functionality and let the user expand/collapse the tree by only clicking on the minus and plus icons such as below.

enter image description here


Solution

  • with plus and minus icons that allows expansion and collapse?

    These are the default icons for the Windows LAF. Other LAF's have different icons.

    You can set your own icons by using the UIManager. See UIManager Defaults.

    Or you can use custom icons for a single JTree only. See Customizing a Tree's Display.

    let the user expand/collapse the tree by only clicking on the minus and plus icons such as below.

    This is the default behaviour.