gpt4 book ai didi

带有监听器的 JavaFX TextArea 给出 : “java.lang.IllegalArgumentException: The start must be <= the end”

转载 作者:行者123 更新时间:2023-12-01 11:18:24 25 4
gpt4 key购买 nike

尝试更改文本属性监听器中的文本时出现异常。异常(exception)情况完全相同是 this question 。我使用 TextArea 而不是 TextField,但我认为错误的来源是相同的。它位于“TextInputControl.java”中。这似乎是 Java 8 的 bug。

@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 票证了吗?

最佳答案

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

编辑:

看看:

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

关于带有监听器的 JavaFX TextArea 给出 : “java.lang.IllegalArgumentException: The start must be <= the end” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31526702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com