Zurf
Zurf

Reputation: 260

How to change color of buttons made from Scene Builder?

I have made a few buttons(each represents a city) on SceneBuilder. I need to set their color according to the number of houses in each city. Darker indicating more properties and lighter less (red).

I have assigned each button a fx:id on scene builder and called it in my code but I am not sure how to change it's color by using javafx code.

Can someone help me out, I am very new to Javafx.

@FXML private Button b1 = new Button();

First i was trying to test wether or not color would actually change but it does not change

@FXML
private void test() {
    for (House s: list) {   
        if(s.getHouse().equals("Manchester") > 10000) {                
            DropShadow s = new DropShadow();
            b1.setEffect(s);        
            b1.setStyle("fx-background-color: #FF0000");
        }
}

}

Upvotes: 2

Views: 15815

Answers (2)

camstu
camstu

Reputation: 51

You can change the buttons color directly through a property rather than manipulating the style.

Here's an example of changing the text in the button to red:

button.setTextFill(Color.RED);

This would be changing the background color:

button.setBackground(new Background(new BackgroundFill(Color.RED, null, null);

Upvotes: 1

Zurf
Zurf

Reputation: 260

As c0oder pointed out, it was a simple mistake.

Change b1.setStyle("fx-background-color: #FF0000"); to b1.setStyle("-fx-background-color: #FF0000");

This did the trick.

Upvotes: 4

Related Questions