How to change click behavior of TreeTableView

I would like to change the click behavior of TreeTableView , but to be honest, I'm not sure what the intended way of doing this should look like, or even if changing it at all.

Most users complained that current behavior would not be consistent. Sometimes a click on a cell already collapses / expands the treeitem, sometimes a double click is required. This, along with initializing the edit mode, can be a pain. Therefore, I would really like to handle click behavior.

I managed to narrow the behavior to the TreeTableCellBehavior class:

 @Override protected void handleClicks(MouseButton button, int clickCount, boolean isAlreadySelected) { // handle editing, which only occurs with the primary mouse button TreeItem<S> treeItem = getControl().getTreeTableRow().getTreeItem(); if (button == MouseButton.PRIMARY) { if (clickCount == 1 && isAlreadySelected) { edit(getControl()); } else if (clickCount == 1) { // cancel editing edit(null); } else if (clickCount == 2 && treeItem.isLeaf()) { // attempt to edit edit(getControl()); } else if (clickCount % 2 == 0) { // try to expand/collapse branch tree item treeItem.setExpanded(! treeItem.isExpanded()); } } } 

Unfortunately, I see no way to create a hook or anything to change this behavior. Since behavior classes and skins are inner classes, I really do not see the possibility of creating my own behavior. So, are there any ideas at what point in the TreeTableView I should connect in order to change the click behavior of the TreeTableView ?


Edit: It seems the code has changed since I posted the code. So I updated this question. I also found api sharing parts, but still couldn't figure out how to overwrite click behavior. Can anyone give me any conclusions?


Edit2: Because of the comment on brians, I created a small example and compared it to the user documentation. It worked as described (=> "Nodes can be opened by clicking the turner icons or double-clicking the node name (note that if editing is enabled, double-clicking will go into editing mode).), So far I have changed TreeColumn TreeTableView to another column and then the first one (which was the only difference between my project and the sample code). Changing the column leads to a strange behavior: the turner no longer responds to single mouseclicks, and sometimes (hardly happens) the selection is not set correctly, instead the node expands / collapses .

I added a small example to demonstrate the problem:

 public class TreeTableViewTurnerIssueExample extends Application { @Override public void start( final Stage primaryStage ) { TreeItem<Item> root = new TreeItem<>( new Item( "Root", "Root" ) ); TreeItem<Item> item1 = new TreeItem<>( new Item( "1", "Item1" ) ); TreeItem<Item> item2 = new TreeItem<>( new Item( "2", "Item2" ) ); TreeItem<Item> item3 = new TreeItem<>( new Item( "3", "Item3" ) ); TreeItem<Item> item4 = new TreeItem<>( new Item( "4", "Item4" ) ); TreeItem<Item> item41 = new TreeItem<>( new Item( "4.1", "Item41" ) ); TreeItem<Item> item42 = new TreeItem<>( new Item( "4.2", "Item42" ) ); TreeItem<Item> item43 = new TreeItem<>( new Item( "4.3", "Item43" ) ); TreeItem<Item> item5 = new TreeItem<>( new Item( "5", "Item5" ) ); TreeItem<Item> item52 = new TreeItem<>( new Item( "5.1", "Item51" ) ); root.getChildren().add( item1 ); item1.getChildren().add( item2 ); root.getChildren().add( item3 ); item3.getChildren().add( item4 ); item4.getChildren().add( item41 ); item4.getChildren().add( item42 ); item4.getChildren().add( item43 ); root.getChildren().add( item5 ); item5.getChildren().add( item52 ); TreeTableColumn<Item, String> nameColumn = new TreeTableColumn<>( "Name" ); TreeTableColumn<Item, String> valueColumn = new TreeTableColumn<>( "Value" ); nameColumn.setCellValueFactory( new TreeItemPropertyValueFactory<Item, String>( "name" ) ); valueColumn.setCellValueFactory( new TreeItemPropertyValueFactory<Item, String>( "value" ) ); final TreeTableView<Item> treeTableView = new TreeTableView<>( root ); treeTableView.getColumns().add( nameColumn ); treeTableView.getColumns().add( valueColumn ); treeTableView.setTreeColumn( valueColumn );//<- changing this to the first column, the clickbehavior is fine. treeTableView.setShowRoot( false ); treeTableView.setColumnResizePolicy( TreeTableView.CONSTRAINED_RESIZE_POLICY ); BorderPane layout = new BorderPane(); layout.setCenter( treeTableView ); Scene scene = new Scene( layout, 400, 400 ); scene.getStylesheets().add( getClass().getResource( "application.css" ).toExternalForm() ); primaryStage.setScene( scene ); primaryStage.show(); } public static void main( final String[] args ) { launch( args ); } } public class Item { private final StringProperty name = new SimpleStringProperty(); private final StringProperty value = new SimpleStringProperty(); public Item( final String name, final String value ) { this.name.set( name ); this.value.set( value ); } public String getName() { return name.get(); } public void setName( final String name ) { this.name.set( name ); } public StringProperty nameProperty() { return name; } public String getValue() { return value.get(); } public void setValue( final String value ) { this.value.set( value ); } public StringProperty valueProperty() { return value; } } 

I also created a Jira Ticket , as I assume it is really a mistake if no one can point me to any mistake I made in this example.

+6
source share
2 answers

It seems that it was an official error fixed in 8u60, as you can see here: Jira

If you still need to change the click behavior in TreeTableView, you probably have to switch from EventFilter , delegating events to your implementation and consumption events. Fortunatly for me, the default behavior will be fine with fixing again, so I stopped further digging.

0
source

Perhaps you can try an event filter. Since I don’t know that your use case is described in detail here, this is a simple error-free example to add a MouseEvent EventHandler to a TreeTableView to extend / reset the TreeItems code by code.

 treeTableView.addEventHandler( MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){ @Override public void handle( MouseEvent e ) { TreeTableCell<Item, String> cell = (TreeTableCell<Item, String>) e.getTarget(); TreeItem<Item> item = cell.getTreeTableView().getTreeItem( cell.getTreeTableRow().getIndex() ); item.setExpanded( !item.isExpanded() ); } }); 
0
source

All Articles