2016-04-15 34 views
0

我尝试在运行程序中设置编辑单元格。设置表可编辑,cellfactory和其他。 我可以编辑单元格,用鼠标点击。但拨打edit()方法TableView不会创建文本框。Javafx tableview编辑方法不能调用cellfactory

我错过了什么?

public class Main extends Application { 

    TableView <TestClass> tableView; 
    TableColumn <TestClass, String> stringColumn; 
    TableColumn <TestClass, String> editColumn; 
    ObservableList<TestClass> items; 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     makeTestData(); 

     tableView = new TableView(); 
     tableView.setEditable(true); 
     stringColumn = new TableColumn<>("Col1"); 
     editColumn = new TableColumn<>("Col2"); 
     tableView.getColumns().addAll(stringColumn, editColumn); 
     stringColumn.setCellValueFactory(cell -> cell.getValue().stringProperty()); 
     editColumn.setCellValueFactory(cell -> cell.getValue().editProperty()); 
     editColumn.setCellFactory(TextFieldTableCell.<TestClass>forTableColumn()); 
     tableView.setItems(items); 

     tableView.getSelectionModel().select(1); 
     tableView.getSelectionModel().focus(1); 
     tableView.edit(1, editColumn); // !!! not create textfield ??? 

     BorderPane pane = new BorderPane(); 
     pane.setCenter(tableView); 
     primaryStage.setScene(new Scene(pane)); 
     primaryStage.show(); 
    } 


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

    public void makeTestData(){ 
     items = FXCollections.observableArrayList(
       new TestClass("str1", "edit1"), 
       new TestClass("str2", "edit2"), 
       new TestClass("str3", "edit3") 
     ); 
    } 

    public class TestClass{ 
     StringProperty string = new SimpleStringProperty(); 
     StringProperty edit = new SimpleStringProperty(); 

     public TestClass() {} 
     public TestClass(String string, String edit) { 
      this.string = new SimpleStringProperty(string); 
      this.edit = new SimpleStringProperty(edit); 
     } 
     public String getString() { return string.get();} 
     public StringProperty stringProperty() { return string; } 
     public void setString(String string) { this.string.set(string);} 
     public String getEdit() { return edit.get();} 
     public StringProperty editProperty() { return edit;} 
     public void setEdit(String edit) { this.edit.set(edit);} 
    } 
} 
+0

无法重现。双击编辑栏中的项目可以显示文本框。 – fabian

+0

确认。使用类似的设置,调用tableview.edit(行,列)确实无法调用可编辑的文本字段。 – 2016-04-20 18:43:09

回答

0

是的,我也得到这个问题。我解决它的方式是通过将编辑方法调用放入另一个fx线程中。

Platform.runLater(() -> { 
    tableView.edit(row, editColumn); 
}); 
相关问题