Tuesday, 5 January 2021

JavaFX: ChoiceDialog

'ChoiceDialog' shows a list of choices to the user, from which they can pick one item at most.

 

'ChoiceDialog' class provides following constructors to define ChoiceDialog instance.

 

public ChoiceDialog()

public ChoiceDialog(T defaultChoice,  @SuppressWarnings("unchecked") T... choices)

public ChoiceDialog(T defaultChoice, Collection<T> choices)

 

Example

List<String> choices = Arrays.asList("Cricket", "Football", "Singing", "Trekking");

ChoiceDialog<String> dialog = new ChoiceDialog<>("Cricket", choices);

 

Find the below working application.

 

ChoiceDialogDemo.java

package com.sample.app.diaogs;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

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

public class ChoiceDialogDemo extends Application {

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

	}

	Button getInformationButton() {
		Button informationButton = new Button("ClickMe");
		informationButton.setAlignment(Pos.CENTER);
		informationButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		informationButton.setPadding(new Insets(10));
		informationButton.setStyle("-fx-background-color:salmon");

		informationButton.setOnAction(event -> {
			List<String> choices = Arrays.asList("Cricket", "Football", "Singing", "Trekking");
			ChoiceDialog<String> dialog = new ChoiceDialog<>("Cricket", choices);
			dialog.setTitle("Hobbies");
			dialog.setHeaderText("User Hobby Selection");
			dialog.setContentText("Please choose your Hobby");

			Optional<String> result = dialog.showAndWait();
			result.ifPresent(letter -> System.out.println("Your hobby: " + letter));

		});

		return informationButton;
	}

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

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

		HBox hBox1 = new HBox();

		Button informationButton = this.getInformationButton();
		informationButton.setEffect(dropShadow);

		hBox1.getChildren().addAll(informationButton);
		hBox1.setPadding(new Insets(20));
		hBox1.setAlignment(Pos.CENTER);
		hBox1.setStyle("-fx-background-color:azure");

		Scene scene = new Scene(hBox1, 300, 200, Color.AZURE);

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

 

Output

 


Click on the button ‘ClickMe’ you will see a choice box.



  

Previous                                                    Next                                                    Home

JavaFX: TextInputDialog

TextInputDialog is used to show a text input control to tehe user.

 

Example

TextInputDialog dialog = new TextInputDialog();

dialog.setTitle("User Name");

dialog.setHeaderText("Look, a Text Input Dialog");

dialog.setContentText("Please enter your name:");

 

 


Find the below working application.

 

TextInputDialogDemo.java

package com.sample.app.diaogs;

import java.util.Optional;

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

public class TextInputDialogDemo extends Application {

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

	}

	Button getInformationButton() {
		Button informationButton = new Button("ClickMe");
		informationButton.setAlignment(Pos.CENTER);
		informationButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		informationButton.setPadding(new Insets(10));
		informationButton.setStyle("-fx-background-color:salmon");

		informationButton.setOnAction(event -> {
			TextInputDialog dialog = new TextInputDialog();
			dialog.setTitle("User Name");
			dialog.setHeaderText("User Registration");
			dialog.setContentText("Please enter your name:");

			Optional<String> result = dialog.showAndWait();

			result.ifPresent(name -> System.out.println("Your name: " + name));

		});

		return informationButton;
	}

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

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

		HBox hBox1 = new HBox();

		Button informationButton = this.getInformationButton();
		informationButton.setEffect(dropShadow);

		hBox1.getChildren().addAll(informationButton);
		hBox1.setPadding(new Insets(20));
		hBox1.setAlignment(Pos.CENTER);
		hBox1.setStyle("-fx-background-color:azure");

		Scene scene = new Scene(hBox1, 300, 200, Color.AZURE);

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


Output





Click on the button ‘ClickMe’, you will see a TextInputDialog.

 


 

Enter your name and click on OK button, you will see a message in console.

 

 


Previous                                                    Next                                                    Home

JavaFX: Dialog with custom actions

Using ButtonType, you can set custom actions to a Dialog box. ButtonType class is used as part of the JavaFX Dialog API (more specifically, the DialogPane API) to specify which buttons should be shown to users in the dialogs.

 


 

In the above example, three button types (Approve, Reject, remindMeLater) added to the Dialog box.

 

Example code snippet

ButtonType approveButtonType = new ButtonType("Approve");

ButtonType rejectButtonType = new ButtonType("Reject");

ButtonType remindMeLaterButtonType = new ButtonType("remindMeLater");

 

dialog.getButtonTypes().setAll(approveButtonType, rejectButtonType, remindMeLaterButtonType);

 

Find the below working application.

 

ButtonTypeDemo.java

package com.sample.app.diaogs;

import java.util.Optional;

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.ButtonType;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class ButtonTypeDemo extends Application {

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

	}

	Button getInformationButton() {
		Button informationButton = new Button("ClickMe");
		informationButton.setAlignment(Pos.CENTER);
		informationButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		informationButton.setPadding(new Insets(10));
		informationButton.setStyle("-fx-background-color:salmon");

		informationButton.setOnAction(event -> {
			Alert dialog = new Alert(AlertType.INFORMATION);
			dialog.setTitle("Dialog Demo");
			dialog.setHeaderText("Simple Dialog");
			dialog.setContentText("You clicked on " + informationButton.getText() + " button");
			dialog.setResizable(true);
			dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
			dialog.getDialogPane().setMinWidth(Region.USE_PREF_SIZE);

			ButtonType approveButtonType = new ButtonType("Approve");
			ButtonType rejectButtonType = new ButtonType("Reject");
			ButtonType remindMeLaterButtonType = new ButtonType("remindMeLater");

			dialog.getButtonTypes().setAll(approveButtonType, rejectButtonType, remindMeLaterButtonType);

			Optional<ButtonType> result = dialog.showAndWait();
			if (result.get() == approveButtonType) {
				System.out.println("Approve Button is clicked");
			} else if (result.get() == rejectButtonType) {
				System.out.println("Reject Button is clicked");
			} else {
				System.out.println("'Remind me later' Button is clicked");
			}

		});

		return informationButton;
	}

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

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

		HBox hBox1 = new HBox();

		Button informationButton = this.getInformationButton();
		informationButton.setEffect(dropShadow);

		hBox1.getChildren().addAll(informationButton);
		hBox1.setPadding(new Insets(20));
		hBox1.setAlignment(Pos.CENTER);
		hBox1.setStyle("-fx-background-color:azure");

		Scene scene = new Scene(hBox1, 300, 200, Color.AZURE);

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


Output





Click on the button ‘ClickMe’. You will see following dialog box.

 

 


Click on a button (Approve, Reject and remindMeLater) of your intrest, same information is logged in console.


Previous                                                    Next                                                    Home

JavaFX: ButtonType

The ButtonType class is used as part of the JavaFX Dialog API (more specifically, the DialogPane API) to specify which buttons should be shown to users in the dialogs.

 


In the above example, three button types (Approve, Reject, remindMeLater) added to the Dialog box.

 

Example code snippet

ButtonType approveButtonType = new ButtonType("Approve");
ButtonType rejectButtonType = new ButtonType("Reject");
ButtonType remindMeLaterButtonType = new ButtonType("remindMeLater");

dialog.getButtonTypes().setAll(approveButtonType, rejectButtonType, remindMeLaterButtonType);

 

Find the below working application.

 

ButtonTypeDemo.java

package com.sample.app.diaogs;

import java.util.Optional;

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.ButtonType;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;

public class ButtonTypeDemo extends Application {

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

	}

	Button getInformationButton() {
		Button informationButton = new Button("ClickMe");
		informationButton.setAlignment(Pos.CENTER);
		informationButton.setFont(Font.font("Verdana", FontPosture.REGULAR, 30));
		informationButton.setPadding(new Insets(10));
		informationButton.setStyle("-fx-background-color:salmon");

		informationButton.setOnAction(event -> {
			Alert dialog = new Alert(AlertType.INFORMATION);
			dialog.setTitle("Dialog Demo");
			dialog.setHeaderText("Simple Dialog");
			dialog.setContentText("You clicked on " + informationButton.getText() + " button");
			dialog.setResizable(true);
			dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
			dialog.getDialogPane().setMinWidth(Region.USE_PREF_SIZE);

			ButtonType approveButtonType = new ButtonType("Approve");
			ButtonType rejectButtonType = new ButtonType("Reject");
			ButtonType remindMeLaterButtonType = new ButtonType("remindMeLater");

			dialog.getButtonTypes().setAll(approveButtonType, rejectButtonType, remindMeLaterButtonType);

			Optional<ButtonType> result = dialog.showAndWait();
			if (result.get() == approveButtonType) {
				System.out.println("Approve Button is clicked");
			} else if (result.get() == rejectButtonType) {
				System.out.println("Reject Button is clicked");
			} else {
				System.out.println("'Remind me later' Button is clicked");
			}

		});

		return informationButton;
	}

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

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

		HBox hBox1 = new HBox();

		Button informationButton = this.getInformationButton();
		informationButton.setEffect(dropShadow);

		hBox1.getChildren().addAll(informationButton);
		hBox1.setPadding(new Insets(20));
		hBox1.setAlignment(Pos.CENTER);
		hBox1.setStyle("-fx-background-color:azure");

		Scene scene = new Scene(hBox1, 300, 200, Color.AZURE);

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

 

Output

 


Click on the button ‘ClickMe’. You will see following dialog box.




Click on a button (Approve, Reject and remindMeLater) of your intrest, same information is logged in console.

 

 

 

 

Previous                                                    Next                                                    Home