Search code examples
javafxjavafx-css

How to add a hover property to button from the javafx controller instead of CSS


Suppose I have a toggle button in JavaFX CSS defined as such.

.btn:hover{
    -fx-background-color: #ffffff;
}

I have several toggle buttons in a FlowPane, but they all need to have different colors that are determined in the controller. How can I change this hover color from the controller instead?


Solution

  • Define a looked-up color in the CSS file:

    .root {
        button-hover-color: #ffffff ;
    }
    .button:hover {
        -fx-background-color: button-hover-color ;
    }
    

    Note that the default CSS actually sets the -fx-color property, from which the background is derived. So if you want to set this up so it behaves in the default way without further modification, experiment with

    .root {
        button-hover-color: -fx-hover-base ;
    }
    .button:hover {
        -fx-color: button-hover-color ;
    }
    

    And then in your controller class you can just change the looked-up color on the buttons (or on any container to apply that to all buttons in the container):

    public class MyController {
        @FXML
        private Button button ;
    
        public void initialize() {
            button.setStyle("button-hover-color: red ;");
        }
    }
    

    Here's a complete example, using the second CSS file above as style.css.

    Main.fxml:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.Button?>
    <?import javafx.geometry.Insets?>
    
    <VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.jamesd.examples.hover.Controller">
        <Button fx:id="firstButton" text="First"/>
        <Button fx:id="secondButton" text="Second"/>
        <Button fx:id="thirdButton" text="Third"/>
    </VBox>
    

    Controller.java:

    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    
    public class Controller {
    
        @FXML
        private Button firstButton ;
        @FXML
        private Button secondButton ;
        @FXML
        private Button thirdButton ;
    
        public void initialize() {
            firstButton.setStyle("button-hover-color: #7fc97f;");
            secondButton.setStyle("button-hover-color: #beaed4;");
            thirdButton.setStyle("button-hover-color: #fdc086;");
        }
    }
    

    and App.java:

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    public class App extends Application {
    
        @Override
        public void start(Stage stage) throws IOException {
            Scene scene = new Scene(FXMLLoader.load(getClass().getResource("Main.fxml")), 640, 480);
            scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
            stage.setScene(scene);
            stage.show();
        }
    
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    

    Note you can also configure the buttons directly in the FXML:

    <Button fx:id="firstButton" text="First" style="button-hover-color: #7fc97f;"/>
    

    etc.