Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Build a JavaFX Application Design a Better App The Controller

Leonard Morrison
PLUS
Leonard Morrison
Courses Plus Student 32,914 Points

I'm having an constructor error.

I keep getting a Invocation exception.

I dunno what's going on. EDIT: The error messages point towards line 20.

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
//        Group root = new Group();
//        Button button = new Button();
//        Text txt = new Text("Sup, man! What's your name?");
//        TextField nameField = new TextField();
//        Label label1 = new Label("Name:");
//        GridPane grid1 = new GridPane();
//        grid1.add(label1, 1, 0);
//        grid1.add(nameField, 2, 0);
//        grid1.add(button, 2, 1);
//        grid1.setHgap(20);
//        grid1.setGridLinesVisible(true);
//        button.setText("Yo!");
//        button.setOnAction(evt -> System.out.printf("Yo! You clicked the right button, %s.%n", nameField.getText()));
//        root.getChildren().add(button);
//        button.setMinSize(25, 25);
//        txt.setY(100);
//        txt.setX(100);
//        VBox box = new VBox();
//        box.getChildren().addAll(txt, grid1);
//        root.getChildren().add(box);
        primaryStage.setTitle("Sup");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
    <Text text="Yo!" GridPane.rowIndex="0" GridPane.columnSpan="2" GridPane.halignment="CENTER"/>
    <Label text="First Name: " GridPane.rowIndex="1" GridPane.columnIndex="0"/>
    <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/>
    <Button text="Sup!" GridPane.columnIndex="2" GridPane.rowIndex="1" GridPane.halignment="RIGHT" onAction="#handleSaySup"/>

</GridPane>
package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.awt.event.ActionEvent;

public class Controller
{
    @FXML private TextField firstName;

    public void handleSaySup(ActionEvent actionEvent)
    {
        System.out.printf("Yo, %s!%n", firstName);
    }


}

I didn't really study javaFX much so this may be from lack of understanding but I don't understand the launch(args) in your main method. I don't see a method launch() anywhere, unless i just missed it. This could possibly have something to do with your issue.

This line here:

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

1 Answer

Thomas Lian Ødegaard
Thomas Lian Ødegaard
9,540 Points

In your Controller.java file you imported awt not javafx.

This

import java.awt.event.ActionEvent;

Should be changed to this

import javafx.event.ActionEvent;