Java: How to programmatically select and expand multiple nodes in a JTree?

前端 未结 1 917
终归单人心
终归单人心 2021-01-04 05:31

I have a JTree and an awt.Canvas. When i select multiple objects from within the Canvas into the objList, I want all the

相关标签:
1条回答
  • 2021-01-04 06:20

    You are looking for the TreeSelectionModel of the JTree (use the getter). Use the TreeSelectionModel#setSelectionPaths for multiple paths. Now you are only setting one node selected due to your tree.setSelectionPath(tpath); call. The TreeSelectionModel also has methods to add/remove to an existing selection ,... (basically everything you might need in the future).

    An interesting method for the expansion is the JTree#setExpandsSelectedPaths method which allows to configure the JTree to automatically expand selected paths. If you want to manage this manually, you can use the JTree#setExpandedState method

    0 讨论(0)
提交回复
热议问题