简体   繁体   中英

Trouble using CSS in JavaFX

I'm new to coding JavaFX and I'm trying to create two checkboxes that will allow a user to remember his username/ password when logging in/out. Coding this isn't the problem; I'm having a hard time graying out the checkboxes (I want the password box to be grayed when the username box isn't checked). Here is a little snippit of code:

    CheckBox rememberUsername = new CheckBox("Remember username?");
    CheckBox rememberPassword = new CheckBox("Remember password?");
    rememberPassword.setStyle("-fx-opacity: 1");

I read an online guide on getting CSS to work, and it said to extract: /jdk1.8.x/jre/lib/ext/jfxrt.jar in C:\\Program Files\\Java, which I did. I'm using Eclipse, and when I load my program, it's as if there's no CSS at all.

Thanks in advance!!

When you disable a control, it will set the opacity to 0.5 (by default) and make it so that the control is not responsive. Opacity of 0.5 will look similar to a grayed out control.

rememberPassword.disableProperty().bind(
    rememberUsername.selectedProperty().not()
);

This is probably what you really want to do, rather than just modifying the CSS style to gray the control out.

In the unlikely event that you don't want to disable the control when you gray it out, you can do:

rememberPassword.opacityProperty().bind(
        Bindings.when(
               rememberUsername.selectedProperty()
       ).then(1).otherwise(0.5)
);

And, in the equally unlikely event that you want to gray it out without disabling it and you want to use inline CSS for this, you can do:

rememberPassword.styleProperty().bind(
        Bindings.when(
                rememberUsername.selectedProperty()
        ).then("fx-opacity: 1;").otherwise("-fx-opacity: 0.5;")
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM