以下工作流程旨在:用户单击按钮 -> 在选定的 TreeItem 处应创建一个新的 TreeItem。选定的 TreeItem 应该会展开,并且应该显示新的 TreeItem 并进入编辑模式,如以下屏幕截图所示:
不幸的是,99% 的编辑模式似乎立即被取消,只是在极少数情况下,单元格仍处于编辑模式。我尝试实现自己的 CellFactories 和 Editable-Cells,但对于这个用例,我总是遇到同样的情况。任何人都知道我在这里做错了什么?
我创建了一个小例子来演示这个案例。
/**
* 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 );
}
}
数据类:
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;
}
}