带有侦听器的JavaFX TextArea给出:“java.lang.IllegalArgumentException:start必须是< = end”

时间:2015-07-20 21:31:29

标签: java exception javafx textarea listener

我在尝试更改文本属性侦听器中的文本时遇到异常。 this question的例外情况完全相同。我使用TextArea而不是TextField,但我认为bug的来源是一样的。它位于“TextInputControl.java”中。这似乎是Java 8的错误。

@FXML
private TextArea ta;
ta.setText("12-");  // pre-set text 
ta.textProperty().addListener((observable, oldValue, newValue) -> {
    ta.setText("1"); // changed text
});

这会导致我的机器出现异常。我在Win 7 64位上有JDK 1.8.0_51。但是,如果我切换回JDK 1.8.0_25,这不会导致异常。

我厌倦了一系列“预设文本”和“更改文本”组合,我发现,一般情况下,如果更改“更改文本”的长度更短,那么“预设”设置文字“。

此问题是否有解决方法?或者我是否在某处犯错?

感谢您的帮助。

修改: 以下代码将重现该问题。我用jre 8u60测试过,问题仍然存在。

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            TextArea ta =  new TextArea();
            ta.setText("12-");  // pre-set text 
            ta.textProperty().addListener((observable, oldValue, newValue) -> {
                ta.setText("1"); // changed text
            });
            AnchorPane root = new AnchorPane();
            root.getChildren().addAll(ta);
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

创建窗口后,TextArea将显示“12-”。通过尝试在任何地方插入字符,TextArea将更改为“1”,但产生以下异常:

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.behavior.TextAreaBehavior.replaceText(TextAreaBehavior.java:305)
    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.TextAreaBehavior.callAction(TextAreaBehavior.java:259)
    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$74(BehaviorBase.java:135)
    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:3964)
    at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
    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$354(GlassViewEventHandler.java:228)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    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:966)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

在更改文本之前清除也无济于事,因为即使只是清除文本,即。 ta.clear();,会导致同样的异常。有任何想法吗?有关于此的JIRA门票吗?

1 个答案:

答案 0 :(得分:1)

对我而言,这似乎是一个错误。从这个版本到另一个版本,第一次发生这样的事情。大部分时间我都会查看JavaFX Jira并查看是否有任何未解决的问题。我此时并没有找到它。作为一种解决方法,我建议使用另一个组件,如果您只想显示文本,可能是Label而不是TextArea

编辑:

看看:

https://bugs.openjdk.java.net/browse/JDK-8081700