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 Build a Pomodoro App Build the State Change Events

Kevin Faust
Kevin Faust
15,353 Points

confused with getStyleClass() and the toString()

Hey everyone,

I am confused with this line of code

container.getStyleClass().add(kind.toString().toLowerCase());

I get how we first select our vBox and we put the enum in string format into our fxml. How would it look like in our fxml? In terms of how would the code look like after we add it (Where in the code is it added to)?

Also when we call the toString() method on the enum, what is returned? We didn't override it so would it return the default memory location like with objects?

Thank you

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey Kevin!

Great question! The toString of an enum returns the current enum's name (or key).

AttemptKind.FOCUS.toString(); // returns "FOCUS"

Make sense?

Kevin Faust
Kevin Faust
15,353 Points

Hey Craig,

Makes sense. Thanks!

Jeremiah Shore
Jeremiah Shore
31,168 Points

Thanks for clarifying! What we were doing wasn't completely clear until I read this and the question below from Amanda Lam and the answer from Benjamin Keller. Thanks everyone.

I couldn't find documentation with .getStyleClass() in it, but what I'm positive about is .getStyleClass().add("class") adds the CSS class to the node, and .getStyleClass().remove("class") would remove it, so

 private void addAttemptStyle(AttemptKind kind) {
        container.getStyleClass().add(kind.toString().toLowerCase());
    }

would mean the class with the same name(lowercased) would effect the node with fx:id="container". For instance FOCUS, would mean the background color is the grey, #2D3339.

.focus {
    -fx-background-color: #2D3339;
}

because the in home.fxml the VBox has the fx:id="container" and is linked to the correct stylesheet.

<VBox stylesheets="/css/main.css"
      xmlns="http://javafx.com/javafx/8"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="com.teamtreehouse.pomodoro.controllers.Home"
      fx:id="container">
Amanda Lam
Amanda Lam
2,178 Points

What does container.getStyleClass() return or what is it suppose to be doing?