2

Following workflow is intended: User clicks a Button -> At the selected TreeItem a new TreeItem should be created. The selected TreeItem should get expanded and the new TreeItem should be displayed and entering in editing-mode, as displayed in the following screenshot:

enter image description here

Unfortunatly the editing-mode seems to be canceled immediately in 99%, just in really rare cases the cell is staying in editing-mode. I tried implementing my own CellFactories and Editable-Cells, but for this usecase I always end up in the same situation. Anyone knows what I am doing wrong here?

I created a small example to demonstrate the case.

/**
 * Small Example for EditableState not initialized Case.
 * 
 * @author mst
 */
public class TreeTableViewCreateExpandAndEditMain extends Application
{
  @Override
  public void start( final Stage primaryStage )
  {
    TreeItem<Item> root = new TreeItem<>( new Item( 1, "Root" ) );

    TreeItem<Item> item1 = new TreeItem<>( new Item( 2, "Item1" ) );
    TreeItem<Item> item11 = new TreeItem<>( new Item( 3, "Item11" ) );
    TreeItem<Item> item2 = new TreeItem<>( new Item( 4, "Item2" ) );
    TreeItem<Item> item3 = new TreeItem<>( new Item( 5, "Item3" ) );
    TreeItem<Item> item31 = new TreeItem<>( new Item( 6, "Item31" ) );

    root.getChildren().add( item1 );
    item1.getChildren().add( item11 );
    root.getChildren().add( item2 );
    root.getChildren().add( item3 );
    item3.getChildren().add( item31 );

    TreeTableColumn<Item, Integer> columnId = new TreeTableColumn<>( "ColumnId" );
    TreeTableColumn<Item, String> columnName = new TreeTableColumn<>( "ColumnName" );

    columnId.setCellValueFactory( new TreeItemPropertyValueFactory<Item, Integer>( "id" ) );
    columnName.setCellValueFactory( new TreeItemPropertyValueFactory<Item, String>( "name" ) );

    columnName.setCellFactory( TextFieldTreeTableCell.forTreeTableColumn() );

    columnName.setOnEditCommit( new EventHandler<CellEditEvent<Item, String>>()
    {
      @Override
      public void handle( final CellEditEvent<Item, String> event )
      {
        final Item item = event.getRowValue().getValue();
        System.out.println( "Change Item " + item + " from " + event.getOldValue() + " to new value "
            + event.getNewValue() );
        item.setName( event.getNewValue() );
      }
    } );

    final TreeTableView<Item> treeTableView = new TreeTableView<>( root );
    treeTableView.getColumns().add( columnName );
    treeTableView.getColumns().add( columnId );
    treeTableView.setShowRoot( false );
    treeTableView.setColumnResizePolicy( TreeTableView.CONSTRAINED_RESIZE_POLICY );

    treeTableView.setEditable( true );

    BorderPane layout = new BorderPane();

    Button button = new Button( "DoAction!" );
    button.setOnAction( new EventHandler<ActionEvent>()
    {
      @Override
      public void handle( final ActionEvent event )
      {
        /*
         * This Method should create a new node at the currently selected node, expand the selected node and start editing the new created node.
         */
        final TreeItem<Item> selectedItem = treeTableView.getSelectionModel().getSelectedItem();

        final TreeItem<Item> newItem =
            new TreeItem<>( new Item( 100, "newItemFor" + selectedItem.getValue().getName() ) );
        selectedItem.getChildren().add( newItem );

        selectedItem.expandedProperty().set( true );
        final int rowIndex = treeTableView.getRow( newItem );

        treeTableView.edit( rowIndex, columnName );//Does 99% not start or get canceled, why?
      }

    } );

    layout.setCenter( treeTableView );
    layout.setBottom( button );

    Scene scene = new Scene( layout, 400, 400 );
    primaryStage.setScene( scene );
    primaryStage.show();
  }

  public static void main( final String[] args )
  {
    launch( args );
  }
}

DataClass:

public class Item
{

  private final IntegerProperty id   = new SimpleIntegerProperty();
  private final StringProperty  name = new SimpleStringProperty();

  public Item( final int id, final String name )
  {
    this.id.set( id );
    this.name.set( name );
  }

  public int getId()
  {
    return id.get();
  }

  public String getName()
  {
    return name.get();
  }

  public void setId( final int id )
  {
    this.id.set( id );
  }

  public void setName( final String name )
  {
    this.name.set( name );
  }

  public IntegerProperty idProperty()
  {
    return id;
  }

  public StringProperty nameProperty()
  {
    return name;
  }
}
crusam
  • 6,140
  • 6
  • 40
  • 68
  • 1
    Your code works every time (5 tries) for me in 1.8.0_20-ea on XP – brian Jun 18 '14 at 16:13
  • oh, thats interesting. In all 5 tries the editmode of the newly created item was directly started, when you clicked the button? I am using 8u5, guess something was fixed in 8u20. Gonna give it a try, thanks! – crusam Jun 19 '14 at 07:17
  • That's right, click button and it's new cell below is focused in editing mode. – brian Jun 19 '14 at 12:13
  • Great, I just tested it, and indeed in 8u20 it works, so I'll just go on with the early access version. Would love to know what exactly changed, but for now thats already good enough. Thank you a lot for testing my example and pointing this out to me. Really appreciate your effort. Feel free to rephrase this for an answer and I'll gladly accept it. – crusam Jun 19 '14 at 12:24

1 Answers1

1

Your code worked in 1.8.0_20-ea on XP for me.

I've often had trouble with the table losing focus and other strange behaviour after edits. Sometimes you have to re-focus the table or cell you want. I keep updating java in hopes it will all work one day.

brian
  • 10,619
  • 4
  • 21
  • 79
  • Could you do me a favour and resize the application-window after you started the application to round about half and/or double of the original size and then push the button to create new nodes? I really get weird results now. Would be really nice, if you just could confirm this. – crusam Jun 20 '14 at 08:35
  • I tried resizing both ways and it still works fine. I find javafx works slower after resizing and there's a bit of pointer lag on my computer. This happens in all javafx programs on my computer (amd X4) – brian Jun 20 '14 at 12:39
  • Thanks for testing. After further research, I submitted a bugreport. For your interest: https://javafx-jira.kenai.com/browse/RT-37634. There really seems to be something going wrong with the indizes under unknown cirumstances. – crusam Jun 20 '14 at 22:57