made textfield numeric

This commit is contained in:
Sem van der Hoeven
2020-03-04 21:40:34 +01:00
parent 3578f38bd2
commit ead13842c8

View File

@@ -2,6 +2,8 @@ package main.java.webcrawler.visualiser;
import javafx.animation.AnimationTimer; import javafx.animation.AnimationTimer;
import javafx.application.Application; import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.HPos; import javafx.geometry.HPos;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
@@ -68,6 +70,7 @@ public class Visualiser extends Application {
TextField wordField = new TextField(); TextField wordField = new TextField();
wordField.setPromptText("Enter the word to search for"); wordField.setPromptText("Enter the word to search for");
TextField amountField = new TextField(); TextField amountField = new TextField();
makeNumeric(amountField);
amountField.setPromptText("Maximum amount of pages the crawler should visit.."); amountField.setPromptText("Maximum amount of pages the crawler should visit..");
VBox content = new VBox(10); VBox content = new VBox(10);
content.setAlignment(Pos.CENTER_LEFT); content.setAlignment(Pos.CENTER_LEFT);
@@ -78,7 +81,7 @@ public class Visualiser extends Application {
urlField, urlField,
new Label("Word to search for:"), new Label("Word to search for:"),
wordField, wordField,
new Label("Maximum amount of pages the crawler should visit:"), new Label("Maximum amount of pages:"),
amountField); amountField);
top.getChildren().add(content); top.getChildren().add(content);
@@ -90,6 +93,19 @@ public class Visualiser extends Application {
} }
private void makeNumeric(TextField textField) {
// force the field to be numeric only
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue,
String newValue) {
if (!newValue.matches("\\d*")) {
textField.setText(newValue.replaceAll("[^\\d]", ""));
}
}
});
}
public void draw(FXGraphics2D graphics) { public void draw(FXGraphics2D graphics) {
graphics.setBackground(new Color(43, 43, 46)); graphics.setBackground(new Color(43, 43, 46));
graphics.clearRect(0, 0, (int) canvas.getWidth(), (int) canvas.getHeight()); graphics.clearRect(0, 0, (int) canvas.getWidth(), (int) canvas.getHeight());