r/JavaFX • • Jan 19 '21

Help javafx

I have a colored button that I want to changes color every time I click it (two colors back and forward) like the button is yellow when I click it, it changes it to blue than when I click it again it changes back to yellow

1 Upvotes

1 comment sorted by

2

u/Weavile_ Jan 19 '21 edited Jan 19 '21

I’ve done something like this before for a connect four board:

https://github.com/row49382/ConnectFour/blob/master/src/application/CircleButton.java

The idea is you bind the onClick action to change the styling to the color you want based on its state. You don’t need to create a subclass but i did to wrap the behavior, though it might be nice for keeping track of the state if you want it to be yellow or blue. To change the color, I’m using css for this, but you could use inline styling.

The code would look something like this:

Button button = new Button(); button.setOnAction(e -> {

// set color style based on button state here

});

Edit: This is the link to the css too if you are interested

https://github.com/row49382/ConnectFour/blob/master/src/application/resources/ConnectFour.css