在javafx TextArea中删除所选文本时出现异常
我想这是javafx的问题,但不是完全确定。请帮帮我! 此代码:在javafx TextArea中删除所选文本时出现异常,java,javafx,textarea,Java,Javafx,Textarea,我想这是javafx的问题,但不是完全确定。请帮帮我! 此代码: package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Over
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<TextArea fx:id="text" prefHeight="900.0" prefWidth="900.0" />
</children>
</GridPane>
这段代码有什么问题?看起来像个bug:您应该在 要解决此问题,请尝试替换
boolean isSelection = text.getSelectedText().length() != 0;
与
这看起来像一个bug:你应该在 要解决此问题,请尝试替换
boolean isSelection = text.getSelectedText().length() != 0;
与
更好的答案是:
boolean isSelection=!text.getSelectText().isEmpty()代码>更好的答案是:
boolean isSelection=!text.getSelectText().isEmpty()代码>退格呢?好的,是的,我可以复制。我想您需要选择文本,以便在删除一些文本后,选择的内容超出剩余文本的长度。这是一个bug…@James\u D我猜在TextInputControl中,文本
会更新,而选择
不会更新,并且会抛出StringIndexOutOfBoundsException,因为IndexRange超出了范围。或者反之亦然?但是像这样的,是的。检查选择
而不是选择的文本
似乎有效。@Elltz是的,退格也有例外退格呢?好的,是的,我可以复制。我想您需要选择文本,以便在删除一些文本后,选择的内容超出剩余文本的长度。这是一个bug…@James\u D我猜在TextInputControl中,文本
会更新,而选择
不会更新,并且会抛出StringIndexOutOfBoundsException,因为IndexRange超出了范围。或者反之亦然?但是像这样的,是的。检查选择
而不是selectedText
似乎有效。@Elltz是的,退格也有例外
boolean isSelection = text.getSelectedText().length() != 0;
boolean isSelection = text.getSelection().getLength() != 0;