Tuesday 24 November 2020

JavaFX: Button Modes

 

A button control has three modes.

 

a.   Normal: A normal push button.

 

b.   Default: A default Button is the button that receives a keyboard VK_ENTER press, if no other node in the scene consumes it.

Ex: button1.setDefaultButton(true);

 

c.    Cancel: A Cancel Button is the button that receives a keyboard VK_ESC press, if no other node in the scene consumes it.

Ex: button2.setCancelButton(true);

 

Find the below working application.

 

ButtonModesDemo.java

package com.sample.app.widgets;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class ButtonModesDemo extends Application {

	public static void main(String args[]) {
		launch(args);

	}

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

		HBox hBox1 = new HBox();
		Label label1 = new Label("Simple button demo");
		label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
		hBox1.getChildren().add(label1);
		hBox1.setPadding(new Insets(20, 20, 20, 20));

		Button button1 = new Button("Enter");
		button1.setAlignment(Pos.CENTER);
		button1.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		button1.setPadding(new Insets(10, 10, 10, 10));
		button1.setDefaultButton(true);

		button1.setOnAction((event) -> {
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("Enter Button Info");
			alert.setHeaderText("Your Action");
			alert.setContentText("Enter button Clicked");

			alert.showAndWait();
		});

		Button button2 = new Button("Cancel");
		button2.setAlignment(Pos.CENTER);
		button2.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		button2.setPadding(new Insets(10, 10, 10, 10));
		button2.setCancelButton(true);

		button2.setOnAction((event) -> {
			Alert alert = new Alert(AlertType.INFORMATION);
			alert.setTitle("Cancel Button Info");
			alert.setHeaderText("Your Action");
			alert.setContentText("Cancel button Clicked");

			alert.showAndWait();
		});

		HBox hBox2 = new HBox();
		hBox2.setSpacing(10);
		hBox2.setPadding(new Insets(20, 20, 20, 20));
		hBox2.getChildren().addAll(button1, button2);

		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);

		VBox vBox = new VBox();
		vBox.getChildren().addAll(hBox1, hBox2);
		vBox.setEffect(dropShadow);

		Scene scene = new Scene(vBox, 500, 500, Color.WHITE);

		primaryStage.setTitle("Button Demo 1");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
}

 


Press Enter button on keyboard, you will observe an event gets triggered and  a dialog box related to Enter button is shown.



Press Esc button on keyboard, you will observe an event gets triggered and  a dialog box related to Cancel button is shown.

 


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment