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 trialprincess mudyahoto
18,502 Pointsbuild a javaFx application
Challenge Task 1 of 1
Let's build an app that is like an old school boom box. There is a play button, when it is clicked, print out to the console "Playing!"
package com.example;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Home extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Group root = new Group();
Button playButton = new Button();
// Add an event handler for playButton
root.getChildren().add(playButton);
primaryStage.setTitle("Boombox");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
3 Answers
Zach Freitag
20,341 PointsSpoiler Alert!
package com.example;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Home extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Group root = new Group();
Button playButton = new Button();
// Add an event handler for playButton
playButton.setOnAction(evt -> System.out.println("Playing!"));
root.getChildren().add(playButton);
primaryStage.setTitle("Boombox");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Duy Pham
Courses Plus Student 44,614 Points. You have to use the method .setOnAction on the Button and then use lambdas to print out the text. You should watch the section "Adding Interactivity" again. At 3:30, Mr.Craig explained very well about that. .By the way, here is the answer: playButton.setOnAction(evt -> System.out.println("Playing!"));
Stewart Mandla Kohlisa
11,737 Pointsit does need anything special,it only requires one line of code
wic is ...... playButton.setOnAction(evt -> System.out.println("Playing!"));