Thursday, 7 January 2021

Module Descriptor: module-info.java

Module is defined by module descriptor file. Module descriptor file contains module name, all the dependent modules and exported packages of this module etc.,

 

Module definition is defined in ‘module-info.java’.

 

module-info.java

module com.helloworld{
   requires java.base;

   exports com.sample.app.util;
}

 

In the above example.

a.   Module name is ‘com.helloworld’. Since modules live in Global namespace, module name must by unique.

b.   ‘requires’ keyword indicates that this module depends on java.base module (Syntax: requires MODULE_NAME).

c.    ‘exports’ keyword indicates that this module exposes ‘com.sample.app.util’ package to outside (exports PACKAGE_NAME).

 

 

 

 

 

Previous                                                    Next                                                    Home

Tuesday, 5 January 2021

JavaFX Tutorial

JavaFX
JavaFX Basics
JavaFX is a high performance graphical user interface library provided by java. It is used to build platform independent, rich UI client applications. Read more>>
JavaFX using fxml
JavaFX is a high performance graphical user interface library provided by java. It is used to build platform independent, rich UI client applications. Read more>>

JavaFX: How to fade a text frame in and out?

Using FadeTransition widget, you can fade a text frame in and out.

 

Step 1: Define a Text widget.

 

Text text = new Text("Hello World");

text.setFill(Color.LIGHTSALMON);

text.setFont(Font.font(null, FontWeight.SEMI_BOLD, 40));

 

Step  2: Apply FadeTransition to the Text widget.

FadeTransition fadeTransition = new FadeTransition(Duration.seconds(3), text);

fadeTransition.setFromValue(2.0);

fadeTransition.setToValue(0.0);

fadeTransition.setCycleCount(Animation.INDEFINITE);

fadeTransition.play();

 

FadeTransitionDemo.java

package com.sample.app;

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FadeTransitionDemo extends Application {

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

		Rectangle rectangle = new Rectangle(300, 100, Color.GRAY);
		rectangle.setArcHeight(30);
		rectangle.setArcWidth(30);

		Text text = new Text("Hello World");
		text.setFill(Color.LIGHTSALMON);
		text.setFont(Font.font(null, FontWeight.SEMI_BOLD, 40));

		FadeTransition fadeTransition = new FadeTransition(Duration.seconds(3), text);
		fadeTransition.setFromValue(2.0);
		fadeTransition.setToValue(0.0);
		fadeTransition.setCycleCount(Animation.INDEFINITE);
		fadeTransition.play();

		StackPane stackPane = new StackPane();
		stackPane.getChildren().addAll(rectangle, text);

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

		primaryStage.setTitle("StackPane Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

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

 


 

  

Previous                                                    Next                                                    Home

JavaFX: How to define rounded rectangle?

Using setArcHeight and setArcWidth methods of Rectangle instance, you can define a rounded rectangle.

 

Rectangle rectangle = new Rectangle(300, 100, Color.GRAY);

rectangle.setArcHeight(30);

rectangle.setArcWidth(30);

 

Find the below working application.

 

RoundedRectangleDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class RoundedRectangleDemo extends Application {

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

		Rectangle rectangle1 = new Rectangle(400, 200, Color.GRAY);
		rectangle1.setArcHeight(30);
		rectangle1.setArcWidth(30);
		
		Rectangle rectangle2 = new Rectangle(300, 100, Color.LIGHTBLUE);
		rectangle2.setArcHeight(30);
		rectangle2.setArcWidth(30);

		StackPane stackPane = new StackPane();
		stackPane.getChildren().addAll(rectangle1, rectangle2);

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

		primaryStage.setTitle("Rounded Rectangle Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

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

 

Run above application, you will see below screen.


 

  

Previous                                                    Next                                                    Home

JavaFX: Example of StackPane

StackPane is used to layout its children in back-to-front stack.

 

In this post, I am going to explain how to add a rectangle and text on top of rectangle using StackPane.

 

Step 1: Define a Rectangle widget.

Rectangle rectangle = new Rectangle(300, 100, Color.GRAY);

rectangle.setArcHeight(30);

rectangle.setArcWidth(30);

 

Step 2: Define a Text widget.

Text text = new Text("Hello World");

text.setFill(Color.LIGHTSALMON);

text.setFont(Font.font(null, FontWeight.SEMI_BOLD, 30));

 

Step 3: Define StackPane widget and add rectangle and text widgets to StackPane.

 

StackPane stackPane = new StackPane();

stackPane.getChildren().addAll(rectangle, text);

 

Step 4: Define a Scene instance and set stacPane ad root node to the Scene widget.

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

 

Step 5: Set Scene instance to Stage instance.

primaryStage.setTitle("StackPane Demo");

primaryStage.setScene(scene);

primaryStage.show();

 

Find the below working application.

 

StackPaneDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class StackPaneDemo extends Application {

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

		Rectangle rectangle = new Rectangle(300, 100, Color.GRAY);
		rectangle.setArcHeight(30);
		rectangle.setArcWidth(30);

		Text text = new Text("Hello World");
		text.setFill(Color.LIGHTSALMON);
		text.setFont(Font.font(null, FontWeight.SEMI_BOLD, 30));

		StackPane stackPane = new StackPane();
		stackPane.getChildren().addAll(rectangle, text);

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

		primaryStage.setTitle("StackPane Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

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

 

Run StackPaneDemo, you will see below screen.

 


  

Previous                                                    Next                                                    Home

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