我在嘗試更改文本屬性偵聽器中的文本時遇到異常。例外情況與this question完全相同。我使用了TextArea而不是TextField,但我認爲bug的來源是相同的。它在「TextInputControl.java」中。這似乎是Java 8的錯誤。帶有偵聽器的JavaFX TextArea給出:「java.lang.IllegalArgumentException:開始必須是<=結束」
@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,則不會引發異常。
我厭倦了一系列「預設文本」和「改變文本」組合,我發現一般情況下,如果它更像是在「改變文本」的長度很短時導致異常,那麼「pre - 設置文本「。
是否有解決此問題的方法?或者我在某處犯了錯誤?
感謝您的幫助。
編輯: 以下代碼將重現此問題。我用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票了嗎?
謝謝你的吉拉信息。我可以使用標籤輸入文字嗎? – MorrisMJ
你想要做什麼?如果您只想顯示文字,該標籤只能作爲替代選擇。你不能使用標籤輸入文本。在你的例子中,你在textarea上有一個監聽器,但是這個想法是什麼。用戶更改文字後,您想要做什麼? – NDY
目前我無法測試它,但是您是否嘗試過使用clear,然後使用appendText(String text)而不是setText來更改textarea中的文本?可能會有效。 – NDY