JavaFX: Applying Shadow Effect

This entry is part 20 of 55 in the series JavaFX

For this post, we will create 2 mouse in and mouse out events and apply a drop shadow effect to a label.

To implement a drop shadow effect, use the DropShadow class. We can specify a color and an offset for the shadow of the label.

Take note of how the DropShadow effect is prepared in line 38 and 39 and was applied to label using method lb.setEffect(ds).

package javafxapplication15;

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

public class JavaFXApplication15 extends Application {

    Label lb = new Label("Every Picture Tells A Story");

    DropShadow ds = new DropShadow();

    public void start(Stage stage) {

        lb.setFont(Font.font("Cambria", 24));

        lb.setOnMouseEntered((MouseEvent e) -> mouseEntered());

        lb.setOnMouseExited(e -> mouseExited());

        BorderPane pane = new BorderPane();
        Scene scene = new Scene(pane, 300, 250);
        stage.setTitle("JavaFX Program");;

    public void mouseEntered() {

        ds.setColor(Color.color(0.4f, 0.4f, 0.4f));



    public void mouseExited() {

        ds.setColor(Color.color(1f, 1f, 1f));


    public static void main(String[] args) {


Try to mouse over the text label to see its effect.


Series Navigation<< JavaFX: Applying Effects To Label
JavaFX: Designing A Button Using CSS >>