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 Controlling Your View

Make sure you attempt to call the handlePlay method on the Play button in boombox.fxml

Okay I wrote the handlePlay mock method for us. Can you wire up the button so that it calls that method when it is clicked please?

com/example/boombox.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox fx:controller="com.example.Controller"
      xmlns:fx="http://javafx.com/fxml">
    <Text text="Boombox" />
    <Button text="Play"/>
    <Button  onAction="handleplay"/> 
</VBox>
com/example/Controller.java
package com.example;

import javafx.event.ActionEvent;

public class Controller {
  public void handlePlay(ActionEvent event) {
    System.out.println("[MUSIC PLAYING] - Take it from me holmes, parents just don't understand");
  }
}
com/example/Main.java
package com.example;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("boombox.fxml"));
        primaryStage.setTitle("Boombox");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

1 Answer

Spoiler Alert!

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox fx:controller="com.example.Controller"
      xmlns:fx="http://javafx.com/fxml">
    <Text text="Boombox" />
    <Button text="Play"
            onAction="#handlePlay" />
</VBox>

Don't forget #