Doug
Doug

Reputation: 7107

Swing JTree - How to use the Netbeans GUI to make the tree single selection?

I'm using Netbeans GUI interface for creating the Swing components.

I've added a JTree to the panel. It appears to be multi-select by default.

Anybody know how in Netbeans I change this to a single-select? I'm not seeing anything exposed in the properties.

Full Answer: Right after "initComponents()" in the constructor, I added the following:

TreeSelectionModel model = jTreeInput.getSelectionModel();
model.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

"jTreeInput" is the name of my jTree.

Upvotes: 1

Views: 2654

Answers (2)

Reimeus
Reimeus

Reputation: 159854

The easiest option is to set it in code:

myJTree.getSelectionModel().setSelectionMode
                       (TreeSelectionModel.SINGLE_TREE_SELECTION);

In Netbeans, JTree has a selectionModel property for this purpose, but requires you to create a custom class derived from TreeSelectionModel with the selection mode set to SINGLE_TREE_SELECTION.

Upvotes: 2

assylias
assylias

Reputation: 328855

I don't know how to do it in netbeans, but you could also write a couple of lines of code:

TreeSelectionModel model = yourJTree.getSelectionModel();
model.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

Upvotes: 2

Related Questions