JavaFX 2.2 TableView:如何在不点击鼠标的情况下编辑表格单元格?

时间:2012-08-21 09:01:28

标签: javafx-2

我遇到了可编辑表格单元格的问题。我在我的项目中使用TableView就像Tutorial on Oracle

根据它,我使用setCellFactory方法在TextFieldTableCell类的帮助下将表格单元格重新实现为文本字段。但是,我发现步骤有点复杂到达可以编辑单元格的位置:

  1. 使用方向键选择表格单元格。
  2. 按“Enter”将单元格转换为文本字段,以便可以对其进行编辑。
  3. 单击文本字段可以编辑内容
  4. 问题在于步骤3,您必须先使用鼠标单击才能在此表格单元格中输入数据。

    那么,有没有解决方案来避免第3步?即只需按“Enter”(步骤2),文本字段就可以输入数据。

    顺便说一下,英语不是我的母语。希望我已经说清楚了。

2 个答案:

答案 0 :(得分:0)

Node can be focused manuallyTextFieldTableCell是一个TableCell,它有一个节点(图形)TextField,当单元格处于编辑模式时,它将被渲染。您需要手动关注此textField,但使用TextFieldTableCell无法访问textField。但是,如果您更喜欢您所指的教程中描述的替代方法,那么您就有机会专注。该教程中唯一改变的方法是:

@Override
public void startEdit() {
    super.startEdit();
    createTextField();
    setText(null);
    setGraphic(textField);
    textField.selectAll();
    // Set the focus
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            textField.requestFocus();
        }
    });
}

答案 1 :(得分:0)

要在没有鼠标单击事件的情况下在TableView中开始编辑,请调用TreeView.edit(rowIndex,tableColumn);

例如:

//create tableview object
TableView<YourModel> tableView = new TableView<>();
//create column
TableColumn<YourModel, String> column = new TableColumn<>("Property Name");
//add column to tableview
tableView.getColumns().add(column);
//... your cell factory and the rest
//add an item
tableView.getItems().add(new YourModel());
//if you want to edit the selected item, get its index
int selectedIndex = tableView.getSelectionModel().getSelectedIndex();
//fire edit
tableView.edit(selectedIndex, column);