Monday 21 May 2018

JavaFX: Button: Add affects to a button

By using 'setEffect' method, you can add effects to a button.

Ex
BoxBlur boxBlur = new BoxBlur();
Button button = new Button("Click Me");
button.setEffect(boxBlur);

Find the below working application.

ButtonApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ButtonApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {

  BoxBlur boxBlur = new BoxBlur();

  Button button = new Button("Click Me");

  VBox vBox = new VBox(10, button);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 400, 400);

  button.setOnMouseEntered((MouseEvent e) -> {
   button.setEffect(boxBlur);
  });

  button.setOnMouseExited((MouseEvent e) -> {
   button.setEffect(null);
  });

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Button Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ButtonApp.class, args);
 }
}

When you ran the application, you can able to see a UI window and a button in the window. When you place the mouse on top of the button, you can observe the button blurness.





Previous                                                 Next                                                 Home

No comments:

Post a Comment