The JavaFX Application Thread throws a StringIndexOutOfBoundsException
if the user pastes a tab character into a TextField
. How can I smartly prevent a user from breaking my application in this way?
Here's a minimal example that demonstrates the behavior.
//Defined in Main.java
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane) FXMLLoader
.load(getClass().getResource("MainView.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Defined in MainView.fxml:
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextField fx:id="tf" />
</children>
</AnchorPane>
Procedure:
\t
) to the clipboardTextField
TextField
The exception occurs whether the user uses the keyboard shortcut (Ctrl+V in Windows) or the context menu. I certainly could add a try-catch block to every TextField
, but that would clutter up the code, and how do I know that this exception is only thrown in this one situation?
Note: This is issue does not appear to affect TextArea
Full text of the exception:
Exception in thread "JavaFX Application Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.AbstractStringBuilder.substring(Unknown Source)
at java.lang.StringBuilder.substring(Unknown Source)
at javafx.scene.control.TextField$TextFieldContent.get(Unknown Source)
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 javafx.scene.control.TextInputControl.replaceText(Unknown Source)
at javafx.scene.control.TextInputControl.replaceSelection(Unknown Source)
at javafx.scene.control.TextInputControl.paste(Unknown Source)
at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.paste(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$74(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.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$353(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$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Well that's ugly. You should report this as a bug.
You can workaround this with a TextFormatter
which filters out tab (and newline) characters. You might want to replace the tab character with, e.g. four spaces.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TextFieldBug extends Application {
@Override
public void start(Stage primaryStage) {
TextField tf = new TextField();
tf.setTextFormatter(new TextFormatter<String>((Change c) -> {
String text = c.getText();
int oldAnchor = c.getAnchor();
int oldCaretPos = c.getCaretPosition() ;
int initialLength = text.length();
text = text.replaceAll("\t", " ");
text = text.replaceAll("\n", "");
c.setText(text);
c.setAnchor(oldAnchor + text.length() - initialLength);
c.setCaretPosition(oldCaretPos + text.length() - initialLength);
return c ;
}));
primaryStage.setScene(new Scene(new StackPane(tf), 350, 120));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With