Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Build a JavaFX Application Build a Pomodoro App Adding Styles

Mohit Kishore
Mohit Kishore
13,935 Points

How to add styles JavaFX

Hello As you can see I am stuck on this part and could I know what I have done wrong ?

com/example/Controller.java
package com.example;

import com.example.model.Brand;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;

public class Controller {
    @FXML
    private VBox container;

    public void handlePlay(ActionEvent actionEvent) {
        System.out.println("[MUSIC PLAYING] - I shot the sheriff, but I didn't shoot the deputy");
    }

    public void switchBrand(Brand brand) {
        clearBrandStyles();
      container.getStyleClass();
      container.add(brand.getDisplayName());
    }

    public void clearBrandStyles() {
        for (Brand brand : Brand.values()) {
            container.getStyleClass().remove(brand.toString().toLowerCase());
        }
    }

    public void handleBrandSwitch(ActionEvent actionEvent) {
        // Get a handle on the calling button
        Button btn = (Button) actionEvent.getSource();
        // Get access to the brand like Brand.APPLE by name
        Brand brand = Brand.valueOf(btn.getText().toUpperCase());
        switchBrand(brand);
    }
}
com/example/boombox.css
/* Brand settings */

.sony {
    -fx-background-color: black;
}

.jvc {
    -fx-background-color: red;
}

.apple {
    -fx-background-color: silver;
}

.coby {
    -fx-background-color: gray;
}
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);
    }
}
com/example/boombox.fxml
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox fx:controller="com.example.Controller"
      xmlns:fx="http://javafx.com/fxml"
      stylesheets="@boombox.css"
      fx:id="container">

    <HBox>
      <Button onAction="#handleBrandSwitch">Sony</Button>
      <Button onAction="#handleBrandSwitch">Apple</Button>
      <Button onAction="#handleBrandSwitch">JVC</Button>
      <Button onAction="#handleBrandSwitch">Coby</Button>
    </HBox>

    <Text text="Boombox" />
    <Button onAction="#handlePlay" text="Play"/>
</VBox>
com/example/model/Brand.java
package com.example.model;

public enum Brand {
    JVC("JVC"),
    SONY("Sony"),
    APPLE("Apple"),
    COBY("Coby");

    private String mDisplayName;

    Brand(String displayName) {
        mDisplayName = displayName;
    }

    public String getDisplayName() {
        return mDisplayName;
    }
}

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Mohit;

You are on the right track, but we need to do some method chaining to get things to work, and convert the display name to lower case.

Something like:

container.getStyleClass().add(brand.getDisplayName().toLowerCase());

Does that make any sense? Post back if you are still stuck.

Happy coding,
Ken