有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

实现可编辑的TableView<ObservableList<String>

因此,我一直在尝试实现一个TableView,在这个TableView中,您只需单击一列即可对其进行编辑,然后按enter键保存编辑。我已经从this线程的答案中获取了很多代码。结果是:

import com.sun.javafx.collections.ObservableListWrapper;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;

public class TestEditableTable extends Application {

    public void start(Stage stage) {

        TableView<ObservableList<String>> tableView = new TableView<ObservableList<String>>();
        tableView.setEditable(true);

        // Some dummy data.
        ObservableList<ObservableList<String>> dummyData = FXCollections.observableArrayList();
        ObservableList<String> firstRow = FXCollections.observableArrayList("Jack", "Smith");
        dummyData.add(firstRow);
        ObservableList<String> secondRow = FXCollections.observableArrayList("Peter", "Smith");
        dummyData.add(secondRow);

        TableColumn<ObservableList<String>, String> firstCol = new TableColumn<ObservableList<String>, String>(
                "First name");
        firstCol.setCellValueFactory(
                (TableColumn.CellDataFeatures<ObservableList<String>, String> param) -> new SimpleStringProperty(
                        param.getValue().get(0)));

        TableColumn<ObservableList<String>, String> secondCol = new TableColumn<ObservableList<String>, String>(
                "Last name");
        secondCol.setCellValueFactory(
                (TableColumn.CellDataFeatures<ObservableList<String>, String> param) -> new SimpleStringProperty(
                        param.getValue().get(1)));

        secondCol.setCellFactory(cell -> new EditableCell());

        tableView.getColumns().addAll(firstCol, secondCol);

        tableView.getItems().addAll(dummyData);

        Scene scene = new Scene(tableView);
        stage.setScene(scene);
        stage.show();
    }

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

    class EditableCell extends TableCell<ObservableList<String>, String> {

        private TextField textfield = new TextField();

        // When the user presses the enter button the edit is saved.
        public EditableCell() {
            setOnKeyPressed(e -> {
                if (e.getCode().equals(KeyCode.ENTER)) {
                    commitEdit(textfield.getText());
                }
            });
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (isEmpty()) {
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    textfield.setText(item);
                    setGraphic(textfield);
                    setText(null);
                } else {
                    setText(item);
                    setGraphic(null);
                }
            }
        }

        @Override
        public void startEdit() {
            super.startEdit();
            textfield.setText(getItem());
            setGraphic(textfield);
            setText(null);
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setGraphic(null);
            setText(getItem());
        }

        @Override
        public void commitEdit(String value) {
            super.commitEdit(value);

            // This works. But gives me a "Discouraged access: The type 'ObservableListWrapper<String>' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_60\lib\ext\jfxrt.jar')". 
            ObservableListWrapper<String> ob = ((ObservableListWrapper<String>) this.getTableRow().getItem());
            ob.set(1, value);

            // I had to put this in a Platform.runLater(), otherwise the textfield remained open.
            Platform.runLater(() -> {
                setText(value);
                setGraphic(null);
            });
        }
    }
}

这个程序运行正常。按下enter按钮时,将编辑该单元格。但这有两个主要问题:

  1. 当我按下enter键并编辑了一个单元格后。我无法通过单击再次编辑它。我需要按另一行才能再次编辑它。设置为将焦点放在行的父项上并没有起作用

  2. commitEdit()内的代码工作正常。但是它很难看,使用ObservableListWrapper会给我一个警告“不鼓励访问:类型'observeListWrapper'不是API(对所需库'C:\Program Files\Java\jre1.8.0\U 60\lib\ext\jfxrt.jar'的限制”)。此外,单元索引是硬编码的,如果我在许多不同的列中使用它,它将不起作用

上述问题均不可接受

最终实现必须支持对textfield中输入的限制。据我所知,这意味着我需要访问单元格中显示的TextField对象,就像我当前的实现一样


共 (2) 个答案

  1. # 1 楼答案

    另一种方法是在构建路径中将JRE库替换为JDK库 项目->;构建路径->;配置生成路径 -1删除JRE库

    -2添加库 -2.1->;选择与JDK环境关联的Java库

    所有错误都将消失

  2. # 2 楼答案

    您正在将事件处理程序注册为单元格上的关键事件处理程序。在文本字段上注册处理程序更有意义,因为这是实际发生感兴趣事件的控件。还请注意TextField在按enter键时的fire操作事件

    因此,替换

        public EditableCell() {
            setOnKeyPressed(e -> {
                if (e.getCode().equals(KeyCode.ENTER)) {
                    commitEdit(textfield.getText());
                }
            });
        }
    

        public EditableCell() {
            textfield.setOnAction(e -> commitEdit(textfield.getText()));
        }
    

    这将确保编辑状态得到正确维护(由于我不太清楚的原因,您的事件处理程序不会出现这种情况),因此它解决了“重新编辑”的问题。这也意味着您不必在commitEdit中手动重置文本和图形

    对于第二个问题,您可以直接向下转换到ObservableList<String>,而不是ObservableListWrapper<String>。但请注意,您也可以这样做

        @Override
        public void commitEdit(String value) {
            super.commitEdit(value);
            ObservableList<String> row = getTableView().getItems().get(getIndex());
            row.set(1, value);
        }
    

    这不仅避免了私有API,而且完全避免了降级