JavaFX: Applying Drop Shadow Effects To Button

This entry is part 22 of 55 in the series JavaFX

Drop shadow effects can be applied to text, label and also button.

As in label, we can use the mouse in and mouse out methods to trigger the event call and apply the effects to the button.

package javafxapplication19;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class JavaFXApplication19 extends Application {
    
    Button btn = new Button("Codecrawl");
    
    DropShadow ds = new DropShadow();
 
    @Override
    public void start(Stage stage) {
 
        stage.setTitle("JavaFX Program!");
 
        StackPane stackpane = new StackPane();
 
        Scene scene = new Scene(stackpane, 300, 250);
        scene.getStylesheets().add("javafxapplication19/style.css");
        stage.setScene(scene);
 

        btn.getStyleClass().add("button1");
        
        btn.setOnMouseEntered(e -> mouseEntered());
        
        btn.setOnMouseExited(e -> mouseExited());
 
        stackpane.getChildren().add(btn);
 
        stage.show();
    }
    
    public void mouseEntered() {
 
        btn.setEffect(ds);
        btn.setText("Mouse In");
 
    }
    
    public void mouseExited() {
 
        btn.setEffect(null);
        btn.setText("Mouse Out");
 
    }
    
    
    public static void main(String[] args) {
        launch(args);
    }
 
}

ds

 

Series Navigation<< JavaFX: Designing A Button Using CSS
JavaFX: Removing CSS effects to a Button >>