JavaFX set background is not working in my Controller

270 views Asked by At

I have tried to do a mouse event in my JavaFX project which is when the mouse entered the background color will change and when the mouse exited will give null, but this error just keeps coming:

The method setBackground(Background) in the type Region is not applicable for the arguments (Color)

The method in my controller class:

public void barcolor(MouseEvent event) {
    menubar.addEventHandler(MouseEvent.MOUSE_ENTERED, e ->
        menubar.setBackground(Color.WHITE)          
    );      
    menubar.addEventHandler(MouseEvent.MOUSE_EXITED, e ->
        menubar.setBackground(null)         
    );      
}
1

There are 1 answers

0
protocol_1903 On
Background mouseOverBackground = new Background(new BackgroundFill(Paint.valueOf("WHITE"),  5, 15));


public void barcolor(MouseEvent event) {
    menubar.addEventHandler(MouseEvent.MOUSE_ENTERED, e ->
        menubar.setBackground(new Background(new BackgroundFill(Paint.valueOf("WHITE"),  5, 15)))          
    );      
    menubar.addEventHandler(MouseEvent.MOUSE_EXITED, e ->
        menubar.setBackground(null)         
    );      
}

You need to make sure that you are passing the right arguments. The setBackground() command only accepts BackgroundFill() and BackgroundImage() as arguments. Either save the background to a variable, or insert it directly into the function.