Hello I want to delete the first line of a JavaFx textarea. I set a TextFormatter on my Textarea and i want to delete the first line, when more then 20 lines are in it:
private <T> TextFormatter<T> createTextFormatter() {
final IntegerProperty lines = new SimpleIntegerProperty(1);
return new TextFormatter<>(change -> {
if (change.isAdded()) {
if (change.getText().indexOf('\n') > -1) {
lines.set(lines.get() + 1);
}
if (lines.get() > 20) {
//TODO
//delete first row
}
}
return change;
});
}
II would be glad if someone could help
Thanks
overriding TextArea's replaceText method and a check for line count than delete first line if it exeeds 20 lines seems working,
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class FixedLineCountTextAreaTry extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Try to enter me more than 20 lines...");
TextArea ta = new TextArea() {
@Override
public void replaceText(int start, int end, String text) {
super.replaceText(start, end, text);
while(getText().split("\n", -1).length > 20) {
int fle = getText().indexOf("\n");
super.replaceText(0, fle+1, "");
}
positionCaret(getText().length());
}
};
StackPane root = new StackPane();
root.getChildren().add(ta);
primaryStage.setScene(new Scene(root, 300, 500));
primaryStage.show();
}
}
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