问题
I am getting the exception when trying to change text in the text property listener. The exception is exactly the same is this question. I used TextArea instead of TextField, but I think the source of the bug is the same. It's in the "TextInputControl.java". It seems to be Java 8's bug.
@FXML
private TextArea ta;
ta.setText("12-"); // pre-set text
ta.textProperty().addListener((observable, oldValue, newValue) -> {
ta.setText("1"); // changed text
});
This causes the exception on my machine. I have JDK 1.8.0_51 on Win 7 64-bit. However, this dosen't cause the exception if I switch back to JDK 1.8.0_25.
I tired a range of "pre-set text" and "changed text" combinations and I found that, generally, if it's more like to cause the exception when the length of "changed text" is short then the "pre-set text".
Is there an workaround for this issue? Or did I make mistakes somewhere?
Thanks for the help.
Edit: The following code will reproduce the issue. I tested with jre 8u60, and the issue still exists.
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);
}
}
After creating the window, the TextArea will show "12-". By trying to insert a character anywhere, the TextArea will change to "1" but with the following exception produced:
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)
Clearing before changing text doesn't help either because even just clearing the text, ie. ta.clear();, would cause the same exception. Any ideas? Is there a JIRA ticket about this already?
回答1:
To me it seems like a bug. Wouldnt be the first time something like this happens from one version to the other. Most of the time I have a look at the JavaFX Jira and see if there are any open issues. I didnt find it at this point. As a workaround I would suggest to use another component, probably the Label
instead of the TextArea
if you only want to display text.
Edit:
Have a look at:
https://bugs.openjdk.java.net/browse/JDK-8081700
来源:https://stackoverflow.com/questions/31526702/javafx-textarea-with-listener-gives-java-lang-illegalargumentexception-the-st