This is my first post on StackOverflow. I'm having an issue within my program for setting up a decimal-hex-binary converter. The idea is to have three textfields: decimal, hex, binary on top of each other(in one VBox) and the labels: decimal, hex, binary, on matching those text field positions. I've acquired that with this code:
GridPane paneForVBoxes = new GridPane();
paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
paneForVBoxes.setVgap(10);
paneForVBoxes.setHgap(10);
VBox units = new VBox(12);
units.setAlignment(Pos.CENTER_LEFT);
Label lbDec = new Label("Decimal");
Label lbHex = new Label("Hex");
Label lbBin = new Label("Binary");
units.getChildren().addAll(lbDec, lbHex, lbBin);
VBox textFields = new VBox(5);
TextField tfDec = new TextField();
tfDec.setPrefColumnCount(19);
tfDec.setAlignment(Pos.CENTER_RIGHT);
TextField tfHex = new TextField();
tfHex.setPrefColumnCount(19);
tfHex.setAlignment(Pos.CENTER_RIGHT);
TextField tfBin = new TextField();
tfBin.setPrefColumnCount(19);
tfBin.setAlignment(Pos.CENTER_RIGHT);
textFields.getChildren().addAll(tfDec, tfHex, tfBin);
paneForVBoxes.add(units, 0, 0);
paneForVBoxes.add(textFields, 1, 0);
So the idea is to then give a live update of conversion to each textfield as you type in a value, for example: In the dec textfield you type 11, in the hex and bin fields 1A and 1011 automatically appear, respectively.
I achieve this to a point, but when I delete all characters in my textfields I receive NumberFormatExceptions and I have no idea how to get around this. This is what I've attempted to fix the problem:
please note This is only the coding for decimal to binary fields, with a simple fix to this I can apply that to the rest of my code that has yet to be developed.
note also I have tried Integer.parseInt(s)
within and around tfDec.setText("0");
but eclipse truly does not like that
tfDec.setOnKeyReleased(e -> {
if (tfDec.equals("")) {
tfDec.setText("0");
}
else {
String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
tfBin.setText(bin);
}
});
/Whole code/
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Elias16_5 extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane paneForVBoxes = new GridPane();
paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
paneForVBoxes.setVgap(10);
paneForVBoxes.setHgap(10);
VBox units = new VBox(12);
units.setAlignment(Pos.CENTER_LEFT);
Label lbDec = new Label("Decimal");
Label lbHex = new Label("Hex");
Label lbBin = new Label("Binary");
units.getChildren().addAll(lbDec, lbHex, lbBin);
VBox textFields = new VBox(5);
TextField tfDec = new TextField();
tfDec.setPrefColumnCount(19);
tfDec.setAlignment(Pos.CENTER_RIGHT);
TextField tfHex = new TextField();
tfHex.setPrefColumnCount(19);
tfHex.setAlignment(Pos.CENTER_RIGHT);
TextField tfBin = new TextField();
tfBin.setPrefColumnCount(19);
tfBin.setAlignment(Pos.CENTER_RIGHT);
textFields.getChildren().addAll(tfDec, tfHex, tfBin);
paneForVBoxes.add(units, 0, 0);
paneForVBoxes.add(textFields, 1, 0);
tfDec.setOnKeyReleased(e -> {
if (tfDec.equals("")) {
tfDec.setText("0");
}
else {
String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
tfBin.setText(bin);
}
});
Scene scene = new Scene(paneForVBoxes, 300, 100);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Data Conversion");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}