2 Replies Latest reply: Mar 8, 2013 10:30 PM by TAJDIN RSS

    Adding textFields values in other textFields

    TAJDIN
      Sir,

      I want to add one TextField's value to an other TextField and want to parse it into double.

      try {  

      amt1.textProperty().addListener(new ChangeListener<String>() {
      public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
      amtTotal.setText(String.valueOf(
      Double.parseDouble(newValue)) );
      }
      });

      } catch (Exception e1) {
      }


      But when I used the code above and want to enter value it (amt1 textField) It gives an error like

      The error is

      java.lang.NumberFormatException: empty String
           at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)

      Also, the cursor hide when I enter e.g; 5 or 0 any digit.

      Pls help
        • 1. Re: Adding textFields values in other textFields
          shakir.gusaroff
          java.lang.NumberFormatException: empty String
          at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)
          You can eliminate entering certain characters by using event filters:

          Also, the cursor hide when I enter e.g; 5 or 0 any digit.
          I did not notice this behavior.

          The following works for me:
          import javafx.application.Application;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.control.TextField;
          import javafx.scene.input.KeyCode;
          import javafx.scene.input.KeyEvent;
          import javafx.scene.layout.StackPane;
          import javafx.scene.layout.VBox;
          import javafx.stage.Stage;
          
          public class TextFields extends Application {
          
              @Override
              public void start(Stage primaryStage) {
          
          
                  VBox root = new VBox();
                  TextField amt1 = new TextField();
                  final TextField amtTotal = new TextField();
          
                  root.getChildren().addAll(amt1, amtTotal);
          
                  amt1.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
                      @Override
                      public void handle(KeyEvent inputevent) {
                          if (inputevent.getCharacter().trim().equals("")) {
                              inputevent.consume();
                          }
                      }
                  });
          
          
          
          
                  amt1.textProperty().addListener(new ChangeListener<String>() {
                      public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
          
                          if (newValue != null && !newValue.isEmpty()) {
                              amtTotal.setText(String.valueOf(
                                      Double.parseDouble(newValue)));
                          }
                      }
                  });
          
                  Scene scene = new Scene(root, 300, 250);
          
                  primaryStage.setTitle("Hello World!");
                  primaryStage.setScene(scene);
                  primaryStage.show();
              }
          
              /**
               * The main() method is ignored in correctly deployed JavaFX application.
               * main() serves only as fallback in case the application can not be
               * launched through deployment artifacts, e.g., in IDEs with limited FX
               * support. NetBeans ignores main().
               *
               * @param args the command line arguments
               */
              public static void main(String[] args) {
                  launch(args);
              }
          }
          • 2. Re: Adding textFields values in other textFields
            TAJDIN
            thanks a lot dear shakir