以下工作流程旨在:用户点击按钮 - >在选定的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;
}
}
答案 0 :(得分:1)
您的代码在我的XP上以1.8.0_20-ea工作。
在编辑后,我经常遇到失去焦点和其他奇怪行为的问题。有时您必须重新聚焦所需的表格或单元格。我一直在更新java,希望它能在一天内完成。