0

So I am writing a javafx program to manipulate the individual bits in a byte. I have a textfield for each bit. I want to implement a changelistener on the textfields so one cannot enter anything but a 0 or a 1. It works fine if the field is empty and the user tries to enter a letter, but if there is already a 0 or 1 in it it throws an exception and I dont understand why.

Here is my code:

public class Task03Controller implements Initializable {
    @FXML private TextField zeroTextField, oneTextField, twoTextField, threeTextField,
                            fourTextField, fiveTextField, sixTextField, sevenTextField;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        zeroTextField.textProperty().addListener((observable, oldValue, newValue) -> {
            if(!zeroTextField.getText().equals("0") && !zeroTextField.getText().equals("1"))
                zeroTextField.clear();
           else if(zeroTextField.getText().length() > 1)
               zeroTextField.setText(zeroTextField.getText().substring(0, 0));
        });
    }
}
xbufu
  • 132
  • 8
  • 2
    You should be using a `TextFormatter`. https://stackoverflow.com/questions/40472668/numeric-textfield-for-integers-in-javafx-8-with-textformatter-and-or-unaryoperat – SedJ601 Mar 02 '20 at 15:01
  • I am relatively new to this, so how exactly do I use a TextFormatter here? Could you give me an example for my case? The code in your answer looks way too complicated – xbufu Mar 02 '20 at 15:13
  • Does this answer your question? [Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator](https://stackoverflow.com/questions/40472668/numeric-textfield-for-integers-in-javafx-8-with-textformatter-and-or-unaryoperat) – SedJ601 Mar 02 '20 at 15:26
  • 1
    @Sedrick Yeah solved it, thanks! – xbufu Mar 02 '20 at 15:32

1 Answers1

0

Using the same idea as the duplicate. You need to define a regular expression that matches binary numbers.

I am using "\\b[01]+\\b" to define binary numbers and "" to define an empty TextField.

MCVE

import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TestingGroundsTwo extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage stage)
    {
        UnaryOperator<Change> binaryFilter = change -> {
            String newText = change.getControlNewText();
            if (newText.matches("\\b[01]+\\b") || newText.matches("")) {
                return change;
            }
            return null;
        };
        TextField textField = new TextField();
        textField.setTextFormatter(new TextFormatter<>(binaryFilter));

        stage.setTitle("Hello World!");
        Scene scene = new Scene(new StackPane(textField), 750, 125);
        scene.setFill(Color.GHOSTWHITE);
        stage.setScene(scene);
        stage.show();
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • 1
    Yeah arrived at almost the same solution, but I used (newText.matches("[0-1]*")) to allow only empty field, 0 or 1 – xbufu Mar 02 '20 at 15:34
  • 1
    @bartman1912 That will allow any number of 0 or 1; if you only want zero or one digit (which I think is the intention), use `"[01]?"` as the regex. – James_D Mar 02 '20 at 15:35