如何对TextFieldTableCell进行每次击键验证? [英] How to do per-keystroke validation of TextFieldTableCell?

查看:43
本文介绍了如何对TextFieldTableCell进行每次击键验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX TextFieldTableCell中按击进行验证,但是我不知道如何从嵌入式TextField控件中捕获文本更改的事件.

I am trying to do per-keystroke validation in a JavaFX TextFieldTableCell but I do not know how to capture the text-changed events from the embedded TextField control.

如果所讨论的对象只是一个TextField,则textField.textProperty().addListener(myChangeListener)可以解决问题. TextFieldTableCell也会公开textProperty(),但是此属性在TextFieldTableCell上的行为完全不同.它不会按击键生成更改事件.相反,当第一次显示TableView时,我会看到很多事件,并且每次在单元格中开始编辑时,都会看到一个事件.

If the object in question were simply a TextField, then textField.textProperty().addListener(myChangeListener) would do the trick. TextFieldTableCell also exposes textProperty(), but this property behaves quite differently on TextFieldTableCell. It does not generate change events on a per-keystroke basis. Rather, I see lots of events when the TableView is first displayed, and I see one event each time I begin editing in a cell.

推荐答案

首先,关于textProperty().

First of all, about textProperty().

看这里看:

http://docs.oracle.com/javafx/2/api/index.html

TextProperty()是带有标签的父类的属性,您不会从中学习任何内容,因为未使用它.棘手的事情是:单元格-标记控件的继承者.在开始编辑时,您会看到TextField,它是单元格(graphicProperty())的图形节点(据我所记得的文档).

TextProperty() is a property of labeled parent class, you will learn nothing from it, because it is not used. It is tricky thing : cell - inheritant of labeled control. TextField, which you see, when start editing, it is a graphic node of cell (graphicProperty()) (as far as I remember documentation).

而且,仅在开始编辑时,此图形节点才由文本字段分配.

And, this graphic node is assigned by a text field, only when editing starts.

AFAIK,无法直接访问可编辑节点.

AFAIK, there is no direct access to editable node.

解决问题的方法-自行实现可编辑单元格.

The way to solve the issue - implement editable cell by your self.

让我与开发人员交谈,以了解更多信息...

Let me talk to developer, to learn more...

假设您有一个DataItem类,其中包含String,并且假定TableView具有封装的数据类型DataItem,并且唯一的列具有相同的封装的数据类型,则可以将此实现用作基础:

Supposing, you have DataItem class, which contains String, and supposing that TableView has encapsulated data type DataItem, and the only column has the same encapsulated data type, you may use this implementation as basis :

    public class TextFieldTableCell extends TableCell<DataItem, DataItem> {

        private TextField textField;

        public TextFieldTableCell() {
        }

        @Override
        public void startEdit() {
            super.startEdit();
            if (isEmpty()) {
                return;
            }

            if (textField == null) {
                createTextBox();
            } else {
                textField.setText(new CellCustomStringConverter().toString(getItem()));
            }

            setGraphic(textField);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

            textField.requestFocus();
            textField.selectAll();
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        }

        @Override
        public void updateItem(DataItem item, boolean empty) {
            super.updateItem(item, empty);
            if (!isEmpty()) {
                if (textField != null) {
                    textField.setText(new CellCustomStringConverter().toString(item));
                }
                setText(item.toString());
            }
        }

        private void createTextBox() {
            textField = new TextField(new CellCustomStringConverter().toString(getItem()));
            textField.setId(TABLE_EDIT_ID);
            textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        commitEdit(new DataItem(textField.getText()));
                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });
        }
    }

这篇关于如何对TextFieldTableCell进行每次击键验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆