0

I have small javafx app and i have two errors. As you can see I dont use fxml. I already tried to clean and build project and then run it again, but still doesnt work. All the solutions I found are related to fxml, but I did not use it. However, I can not find where I'm wrong. Can somebody help me?

Here is my code:

Label name = new Label("Name: ");
TextField txName = new TextField();
Label lastname = new Label("LastName  ");
TextField txLastName = new TextField();
Label age = new Label("Age: ");
TextField txAge = new TextField();
Label phoneNumber = new Label("Phone number: ");
TextField txPhoneNumber = new TextField();
Label numberCarLicence = new Label("Car licence number: ");
TextField txNumberCarLicence = new TextField();

  Label choseCar = new Label("Chose car: ");
  TextField txChoseCar = new TextField();
  Label pickLocation = new Label("Pick location: ");
  TextField txPickLocation = new TextField();
  Label backLocation = new Label("Back location: ");
  TextField txBackLocation = new TextField();
  Label pickDays = new Label("Pick days: ");
  TextField txPickDays = new TextField();

GridPane center = new GridPane();
Button btnRent = new Button("Rent");
TableView tableView = new TableView();

@Override
public void start(Stage primaryStage) {

    UnaryOperator<TextFormatter.Change> phoneNumberJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter = new TextFormatter<>(phoneNumberJustNumbers);
    txPhoneNumber.setTextFormatter(textFormatter);

    UnaryOperator<TextFormatter.Change> numberCarLicenceJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter1 = new TextFormatter<>(numberCarLicenceJustNumbers);
    txNumberCarLicence.setTextFormatter(textFormatter1);

    UnaryOperator<TextFormatter.Change> pickDaysJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter2 = new TextFormatter<>(pickDaysJustNumbers);
    txPickDays.setTextFormatter(textFormatter2);

    UnaryOperator<TextFormatter.Change> ageJustNumbers = change -> {
        String text = change.getText();
        if (text.matches("[0-9]*")) {
            return change;
        }
        return null;
    };
    TextFormatter<String> textFormatter3 = new TextFormatter<>(ageJustNumbers);
    txAge.setTextFormatter(textFormatter3);

    Scene scene = new Scene(center, 1000, 400);

    txChoseCar.setEditable(false);
    txChoseCar.setStyle("-fx-background-color : lightgray");
    txPickLocation.setEditable(false);
    txPickLocation.setStyle("-fx-background-color : lightgray");
    txBackLocation.setEditable(false);
    txBackLocation.setStyle("-fx-background-color : lightgray");
    txPickDays.setEditable(false);
    txPickDays.setStyle("-fx-background-color : lightgray");

    primaryStage.setTitle("Rent a car");
    primaryStage.setScene(scene);
    primaryStage.show();

    center.add(name, 0, 0);
    center.add(txName, 1, 0);
    center.add(lastname, 0, 1);
    center.add(txLastName, 1, 1);
    center.add(age, 0, 2);
    center.add(txPhoneNumber, 1, 2);
    center.add(phoneNumber, 0, 3);
    center.add(txPhoneNumber, 1, 3);
    center.add(numberCarLicence, 0, 4);
    center.add(txNumberCarLicence, 1, 4);
    center.add(choseCar, 3, 0);
    center.add(txChoseCar, 4, 0);
    center.add(pickLocation, 3, 1);
    center.add(txPickLocation, 4, 1);
    center.add(backLocation, 3, 2);
    center.add(txBackLocation, 4, 2);
    center.add(pickDays, 3, 3);
    center.add(txPickDays, 4, 3);
    center.add(btnRent, 5, 6);

    center.setPadding(new Insets(10, 5, 10, 5));
    center.setAlignment(Pos.CENTER);
    center.setVgap(10);
    center.setHgap(10);

    alert.show();

}

public static void main(String[] args) {
    launch(args);
}

}

Stack Trace:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
    at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
    at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
    at javafx.scene.layout.GridPane.add(GridPane.java:965)
    at cs102projekat.CS102Projekat.start(CS102Projekat.java:125)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
12
  • 1
    .fxml is not in the classpath. Commented Jul 29, 2019 at 16:43
  • So how I can add fxml Commented Jul 29, 2019 at 16:46
  • Are you using any ide like Eclipse or Intellij Idea ? Commented Jul 29, 2019 at 16:56
  • NetBeans IDE 8.2 Commented Jul 29, 2019 at 16:56
  • 1
    Note an InvocationTargetException is not specific to FXML or even JavaFX. That exception is thrown when a constructor or method is invoked via reflection and said constructor or method throws an exception—the InvocationTargetException wraps the cause. Due to this, you must look at the Caused by:s of such an exception to get the fundamental error (in your case, an IllegalArgumentException). Commented Jul 30, 2019 at 1:11

1 Answer 1

2

The StackTrace points to the actual issue:

Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT

Each Node may only be added to a Scene graph once. In your case, you call center.add(txPhoneNumber...) twice.

When trying to add it the second time, the compiler will fail and give the IllegalArgumentException you're seeing.

Remove the second attempt to add txPhoneNumber and this error will go away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.