How to get JTree expanded?

Posted by Dzmitry Zhaleznichenka on Stack Overflow See other posts from Stack Overflow or by Dzmitry Zhaleznichenka
Published on 2010-06-02T08:03:25Z Indexed on 2010/06/02 13:54 UTC
Read the original article Hit count: 195

Filed under:
|
|

I have a wizard with several screens where user has to fill his/her details for further processing. At the second screen I have a radio group with three radio buttons that enable additional elements. To proceed, user has to choose one of them. When user selects third button, single-selection JTree filled in with data enables and user has to select an option from it. Then user has to press "Next" to get to next screen. The option he\she had selected is stored as a TreePath. So far so good.

My problem is the following. If the user wants to come back from the following screen to the screen with a JTree, I want to provide him\her with the JTree expanded to the option that had been selected and to highlight the option. However, whatsoever I try to do for that (any combinations of expandPath, scrollPathToVisible, addSelectionPath, makeVisible) always provides me with a collapsed tree. I try to expand both leaves and nodes. My code looks like this:

rbProcessJTree.setSelected(isProcessJTree());

if (null != getSelectedTablePath()){
    trTables.addSelectionPath(getSelectedTablePath());
    trTables.expandPath(getSelectedTablePath());
    trTables.scrollPathToVisible(getSelectedTablePath());
}

When setSelected() is called, state change listener is invoked that enables JTree. The model is loaded during the form initialization.

Each time I switch between screens, I save the input data from previous screen and dispose it. Then, when I need to open previous screen back, I save data from the following screen, dispose it, load data to this screen and show it. So each time the screen is generating from scratch.

Could you please explain, what sequence of operations has to be done to get JTree expanded in a newly created form,with data model loaded and selection path provided?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing