search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A Dynamically modify inline CSS styles in Javafx
Dynamically modify inline CSS styles in Javafx
P粉311089279 2024-04-06 20:57:16 Views 1676

0

I'm trying to change the background image of a pane when the application is maximized. My background is set using inline css. I set up two different variables and an if statement for the style. However, I'm having no luck getting it to change style.

String cssStyle = "-fx-background-image: url(\'file:images/poker_table.png\');" +
                 "-fx-background-position: center center;" +
                 "-fx-background-radius: 15;" + // ************* For rounded corners
                 "-fx-background-size: 100% 100%;";
String cssStyle2 = "-fx-background-image: url(\'file:images/poker_table.jpg\');" +
                  "-fx-background-position: center center;" +
                  "-fx-background-radius: 15;" +
                  "-fx-background-size: 100% 100%;";
if (!primaryStage.isMaximized())
{   gameScreen.setStyle(cssStyle);
}
else
{   gameScreen.setStyle(cssStyle2);
}
Your Answer
submit

1 answers
2

Just add a listener to the Stage's maximizedProperty(). Properties and listeners are a fundamental part of the JavaFX API: you can find them in the Standards Documentation, or any good JavaFX tutorial.

primaryStage.maximizedProperty().addListener((obs, wasMaximized, isNowMaximized) -> {
    if (isNowMaximized) {
        gameScreen.setStyle(cssStyle2);
    } else {
        gameScreen.setStyle(cssStyle);
    }
});

You may also want to set appropriate styles immediately using existing code.

You can also use binding if you prefer:

gameScreen.styleProperty().bind(Bindings.createStringBinding(
    () -> primaryStage.isMaximized() ? cssStyle2 : cssStyle,
    primaryStage.maximizedProperty()
);

Binding replaces your existing code; it is applied immediately and when maxmizedProperty changes.

2024-04-07 19:47:58

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use