如何在JavaFX TextField中获取结束事件

huangapple go评论69阅读模式
英文:

How i can get the end event in javafx TextField

问题

我有一个文本字段,通过扫描仪读取的条形码会传递一个数字,但我不知道条形码有多少位数字,所以我想知道事件何时完成,以获取完整的扫描数字。

以下是代码:

txSerialId.textProperty().addListener((obs, oldText, newText) -> {

    System.out.println("addListener..........obs    : " + obs);
    System.out.println("addListener..........oldText: " + oldText);
    System.out.println("addListener..........newText: " + newText);
});

我正在使用JavaFx 8。

英文:

I have a text field that receives a number read from barcode by scanner, but i don't know how many digits the barcode has, so, I would like to know when the event was finished, for get the complete number scanned.

Here is the code

txSerialId.textProperty().addListener((obs, oldText, newText) -> {
			
			System.out.println("addListener..........obs    : " + obs);
			System.out.println("addListener..........oldText: " + oldText);
			System.out.println("addListener..........newText: " + newText);
});

I am using JavaFx 8.

答案1

得分: 1

以下是翻译好的内容:

目前我所知,没有特别好的方法来处理这个,因为在将其读入文本字段时,没有任何指示“扫描结束”的内容(除非您提前知道条形码文本的长度)。

我能提出的最佳建议是等待一段时间内没有输入更改。这应该有效,因为文本将被扫描仪快速读取;但这会导致在响应扫描时有轻微延迟。

您可以使用`PauseTransition`来实现这一点,并在暂停完成时更新`StringProperty`:

final PauseTransition scannerDelay = new PauseTransition(Duration.seconds(0.25));
final StringProperty barcode = new SimpleStringProperty();
// 当延迟完成时,使用文本字段中的文本更新条形码:
scannerDelay.setOnFinished(event -> barcode.set(txSerialId.getText()));
// 当文本字段中的文本更改时,启动(或重新启动)延迟:
txSerialId.textProperty().addListener((obs, oldText, newText) ->
    scannerDelay.playFromStart());

// 响应条形码更改:
barcode.addListener((obs, oldBarcode, newBarcode) ->
    System.out.printf("条形码从%s更改为%s %n", oldBarcode, newBarcode));
英文:

There's no particularly nice way to do this, as far as I know, because there's nothing that indicates the "end of scan" when reading this into a text field (unless you know ahead of time the length of the barcode text).

The best I can suggest is to wait until no changes have been input for some short period of time. This should work, because the text will be read by the scanner rapidly; it comes at the cost of a slight delay in reacting to the scan.

You can do this using a PauseTransition, and update a StringProperty when the pause completes:

final PauseTransition scannerDelay = new PauseTransition(Duration.seconds(0.25));
final StringProperty barcode = new SimpleStringProperty();
// when delay finishes, update barcode with text in text field:
scannerDelay.setOnFinished(event -> barcode.set(txSerialId.getText());
// when text in text field changes, start (or restart) the pause:
txSerialId.textProperty().addListener((obs, oldText, newText) -> 
    scannerDelay.playFromStart());

// react to changes in barcode:
barcode.addListener((obs, oldBarcode, newBarcode) -> 
    System.out.printf("Barcode changed from %s to %s %n", oldBarcode, newBarcode));

huangapple
  • 本文由 发表于 2020年8月10日 22:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63342680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定