Changing javafx button color on click?

后端 未结 2 1047
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 08:00

I know I can set a color by using the pressed pseudo selector:

myButton:pressed{}

Problem is, im trying to do this in code by overriding th

相关标签:
2条回答
  • 2021-01-06 08:16

    Styling FX Buttons with CSS show some applicable style options for a button.

    "fx-background-color" is just a typo. It needs to be "-fx-background-color".

    To use the styles you need to get the style names and values correct and separate them with semicola. The following approach does this systematically:

      String bstyle=String.format("-fx-text-fill: %s;-fx-fill: %s;-fx-background-color: %s;",textFill,fill, bgColor);
      button.setStyle(bstyle);
    
    0 讨论(0)
  • 2021-01-06 08:35

    I had to do a similar thing(here is simplified, there is just the part of code which change the style to the button) and i did this, i hope it will be helpful to you

    button.setOnAction((ActionEvent e) -> {
        button.getStyleClass().removeAll("addBobOk, focus"); 
        //In this way you're sure you have no styles applied to your object button
        button.getStyleClass().add("addBobOk");
        //then you specify the class you would give to the button
    });
    

    CSS:

    .addBobOk{
            -fx-background-color:#90EE90;
            -fx-background-radius: 5,5,4;
            -fx-background-insets: 0 0 -1 0,0,1;
            -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
            -fx-text-alignment: center;
    }
    .addBobOk:hover{
    
            -fx-background-color:#64EE64;
            -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.4) , 5, 0.0 , 0 , 1 );
            -fx-text-alignment: center;
    }
    .busy{
    
            -fx-background-color:#B3B3B3; 
            -fx-text-alignment: center;
    }
    .busy:hover{
    
            -fx-background-color:cdcbcb;                       
            -fx-text-alignment: center;
    }
    
    0 讨论(0)
提交回复
热议问题