2

Hi everyone,

I'm currently making a slide puzzle game using JavaFX:

enter image description here

To play the game, you click buttons next to the empty box to switch the two, and the point of the game is to get all the buttons in numerical order from 1 to 15 in the least amount of moves possible. I finished coding the game for the most part (still have a few features I want to add), but now I want to style the buttons using CSS.

But here's the thing: there is a boolean value for each button that determines if it can be moved (true if it's next to the emptyButton, false otherwise). How can I style the buttons so that if their corresponding boolean value is true, the background color is green, and red otherwise? I'm not too skilled in CSS so any help would be much appreciated. Thanks!

3
  • Forgot to mention, these boolean values change every time a button is clicked. (I basically want all the buttons directly next to or above/below the empty button to have a green background). Is this possible? Commented Apr 16, 2017 at 1:13
  • One way to do this is with a CSS pseudo-class. See docs.oracle.com/javase/8/javafx/api/javafx/scene/… . Commented Apr 16, 2017 at 1:26
  • I'll try it, thanks for the recommendation! Commented Apr 16, 2017 at 1:43

1 Answer 1

3

The easiest way is by creating pseudoclass, as mentioned, here is an example:

private PseudoClass empty = PseudoClass.getPseudoClass("empty");

in the button change add:

pseudoClassStateChanged(empty,isNextToEmpty(sampleButton));

given that the method isNextToEmpty() returns the Boolean value you added to the button.

In a css file:

.button:empty {
-fx-background-color: green;}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.