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 trialChance Edward
4,507 PointsIs there something wrong with my code? It's not working the way it's supposed to.
Here is the Controller.java
Controller.java {
package sample.sup;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class Controller {
@FXML private TextField firstName;
public void handleSaySup(ActionEvent actionEvent) {
System.out.printf("Sup %s %n!", firstName.getText());
}
}
}
here is Main.java
Main.java{
package sample.sup;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.Stage;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
/*Group root = new Group();
Text txt = new Text("Sup");
txt.setFont(new Font("Bernard MT Condensed", 80));
Label label = new Label("Name:");
TextField nameFld = new TextField();
GridPane grid = new GridPane();
grid.add(label, 0, 0);
grid.add(nameFld, 1, 0);
grid.setHgap(20);
Button btn = new Button();
grid.add(btn, 1, 1);
//grid.setGridLinesVisible(true);
btn.setText("Say Sup");
btn.setOnAction(evt -> System.out.printf("Sup %s!%n",
nameFld.getText()));
txt.setY(50);
VBox box = new VBox();
box.getChildren().addAll(txt, grid);
root.getChildren().add(box);*/
primaryStage.setTitle("Sup");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args); }
}
}
Here is sample.css
sample.css{
.title {
-fx-font-size: 80px;
-fx-fill: AQUA
}
}
And lastly sample.fxml
sample.fxml{
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>
<GridPane fx:controller="sample.sup.Controller"
stylesheets="/css/sample.css"
xmlns:fx="http//javafx.com/fxml"
style="-fx-font-family: Bernard MT Condensed; -fx-alignment: center; -fx-hgap: 10; -fx-vgap: 10"
<Text text="Sup"
styleClass="title"
GridPane.rowIndex="0"
GridPane.columnSpan="2"
GridPane.halignment="CENTER"/>
<Label text="First Name:"
GridPane.rowIndex="1"
GridPane.columnSpan="0"/>
<TextField fx:id="firstName"
GridPane.columnIndex="1"
GridPane.rowIndex="1"/>
<Button text="Say Sup!"
onAction="#handleSaySup"
GridPane.rowIndex="2"
GridPane.columnIndex="1"
GridPane.halignment="RIGHT"
/>
</GridPane>
}