Pages

Thursday, May 10, 2012

JavaFx - Button - showing a Message, styling and some effects.

A small code to present, how can we work with buttons in JavaFx:





package javafxapplication_button;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
 *
 * @author Sipos Lehel
 */
public class JavaFXApplication_Button extends Application {

    Button btn = null;
    DropShadow shadow = null;  
   
    public static void main(String[] args) {
        launch(args);
    }
   
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
       
       
        //button icon
        Image imageDecline = new Image(getClass().getResourceAsStream("desktop.gif"));
        btn = new Button("OK");
        btn.setGraphic(new ImageView(imageDecline));       
       
       
        //effects at mouse moving upon the button
        shadow = new DropShadow();
        //Adding the shadow when the mouse cursor is on and zooming in
        btn.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent e) {
                btn.setEffect(shadow);
                btn.setScaleX(1.5);
                btn.setScaleY(1.5);                
            }
        });
        //Removing the shadow when the mouse cursor is off and zooming out
        btn.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent e) {
                btn.setEffect(null);
                btn.setScaleX(1);
                btn.setScaleY(1);                                
            }
        });       
       
        //on click event - fire a Dialog Box message
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                Stage dialogStage = new Stage();
                dialogStage.initModality(Modality.WINDOW_MODAL);
                StackPane panel = new StackPane();
                panel.getChildren().add(new Text("This is a message for you: Hello world!"));
                Scene scene_message = new Scene(panel);
                dialogStage.setScene(scene_message);
                dialogStage.setHeight(100);
                dialogStage.setWidth(300);
                dialogStage.show();

            }
        });       
       
        //styling the button
        btn.setStyle("-fx-font: 22 arial; -fx-text-base-color: blue; -fx-base: #b6e7c9");       
       
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}





No comments:

Post a Comment