本文介绍了具有监听器的JavaFX Textfield给出:“java.lang.IllegalArgumentException:起始必须是=结束”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有异常,不明白是什么原因。它是一个FX应用程序与FXML文件。在App的init()部分,我将一个监听器添加到一个文本框中,该文本框调用一个函数,该函数监视一个模式的正则表达式的String。

i am getting an exception and don't understand what causes it. It's an FX App with FXML files. At the init() part of the App i add a listener to a textfield which calls a function that monitors the String with a regex for a pattern.

tf.textProperty().addListener(
        (observable, oldValue, newValue) -> {
            handleInput(newValue);
        });

功能:

private void handleInput(String s) {

            s = s.toUpperCase();
            Matcher matcher = Pattern
                    .compile(
                            "^[A-Z]{2}(20|21|22|23|[0-1]\\d)[0-5]\\d(20|21|22|23|[0-1]\\d)[0-5]\\d(T\\s|C\\s|TC|CT|\\s\\s)$")
                    .matcher(s);

            if (matcher.find()) {

                    // do something

                    // then clear the textfield
                    tf.clear();

                } else {
                    // do something else
                }
            }

它的工作原理,但是如果匹配器匹配,会给我一个异常。

It works but gives me an exception in case the matcher matches.

异常:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: The start must be <= the end
    at javafx.scene.control.TextInputControl.getText(Unknown Source)
    at javafx.scene.control.TextInputControl.updateContent(Unknown Source)
    at javafx.scene.control.TextInputControl.replaceText(Unknown Source)
    at com.sun.javafx.scene.control.skin.TextFieldSkin.replaceText(Unknown Source)
    at com.sun.javafx.scene.control.behavior.TextFieldBehavior.replaceText(Unknown Source)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.defaultKeyTyped(Unknown Source)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(Unknown Source)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(Unknown Source)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(Unknown Source)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$75(Unknown Source)
    at com.sun.javafx.scene.control.behavior.BehaviorBase$$Lambda$88/1978656397.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$KeyHandler.process(Unknown Source)
    at javafx.scene.Scene$KeyHandler.access$1800(Unknown Source)
    at javafx.scene.Scene.impl_processKeyEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$349(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$349/1135388943.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(Unknown Source)
    at com.sun.glass.ui.View.handleKeyEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyKey(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)}

我已经尝试了一些我在其他帖子中发现的建议,比如在将String赋给匹配器:

i already tried some suggestions which i found here in other posts, like to check the String before giving it to the matcher:

if (!tf.getText() == null) {

if (!tf.getText().equals("") {

但异常仍然存在。当我删除:

but the exception remains. When i remove:

tf.clear();

一切正常。

任何想法任何人?

编辑swinkler的解决方案:

我使用swinkler的代码时得到的源代码 - JDK 1.8.0_45:

the exception with sources i get when using swinkler's code - JDK 1.8.0_45:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: The start must be <= the end
    at javafx.scene.control.TextInputControl.getText(TextInputControl.java:446)
    at javafx.scene.control.TextInputControl.updateContent(TextInputControl.java:564)
    at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:548)
    at com.sun.javafx.scene.control.skin.TextFieldSkin.replaceText(TextFieldSkin.java:576)
    at com.sun.javafx.scene.control.behavior.TextFieldBehavior.replaceText(TextFieldBehavior.java:202)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.defaultKeyTyped(TextInputControlBehavior.java:238)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:139)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:127)
    at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda$new$75(BehaviorBase.java:135)
    at com.sun.javafx.scene.control.behavior.BehaviorBase$$Lambda$91/476337053.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3965)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3911)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2502)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:197)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:147)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$349(GlassViewEventHandler.java:228)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$142/181559839.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:227)
    at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
    at com.sun.glass.ui.View.notifyKey(View.java:956)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)


推荐答案

我有相同的,并发现错误报告,解决了这个问题。它不是一个bug。

I had the same problem and found the bug report JDK-8081700, which solved the problem. It turns out not a bug.

正确的方法是应用程序延迟修改,
使用runLater():

The correct way is for the application to delay the modification, using runLater():

    if (matcher.find()) {
        System.err.println("true");
        Platform.runLater(() -> {
            textField.clear();
        });
    }


这篇关于具有监听器的JavaFX Textfield给出:“java.lang.IllegalArgumentException:起始必须是=结束”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:02