2

Is it possible to activate multiple selection for a JavaFX TreeView with FXML?

If you want to set the selection mode programmatically, you would call

treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

But how can that be described in FXML? The best I came up with for now was:

<?import javafx.scene.control.TreeView.TreeViewBitSetSelectionModel?>
...
<TreeView>
  <selectionModel>
    <TreeViewBitSetSelectionModel selectionMode="MULTIPLE"/>
  </selectionModel>
</TreeView>

This leads to this exception:

javafx.fxml.LoadException: TreeViewBitSetSelectionModel is not a valid type.

I assume that this is either because is not a public class or because it only has a non default constructor.

I read in the FXML documentation, that there are builders for various types, but I did not see a Builder or a Factory for selection model classes. Did I miss this?

Any hints on how to achieve this are very welcome!

christoph.keimel
  • 1,240
  • 10
  • 24
  • It is not a public class. I don't think you can do this in FXML (other than implementing your own selection model, which is a lot of work): you have to do it in the controller. – James_D Nov 11 '15 at 13:22
  • @James_D That was my conclusion as well. I was hoping there might be a different way. – christoph.keimel Nov 11 '15 at 14:00
  • Surely it's not that big a deal to configure the selection model in the controller? That's arguably the correct place to be doing that anyway. – James_D Nov 11 '15 at 14:04
  • Well ... I have a declarative UI model (wxDesigner) that needs to be converted to FXML at runtime using XSLT. In the source model it was possible to define the selection mode of a tree, so I need to use this information somehow. My work around for now is to use the userData property of the TreeView to save this information. I then use this in the controller to modify the selection model. – christoph.keimel Nov 12 '15 at 14:41
  • 1
    Cool... At some point you're likely to need to get some information to the controller which you can't specify in the FXML in the natural way. `userData` is one option: another might be to generate an [``](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#define_elements) block and put some values in there (perhaps a map with key-value pairs that you can parse out in the controller, etc). Also be aware you can access the [namespace](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader.html#getNamespace--) which has the `fx:id`s. – James_D Nov 12 '15 at 14:50

0 Answers0