Thursday, 7 January 2021

Java9: Qualified Exports: Export to specific modules

In my previous posts, I explained how a module expose packages to other modules using ‘exports’ keyword.

 

There are cases, where you want to expose packages to specific modules. You can do this by using qualified exports in module descriptor.

 

Syntax

module {module_name} {
  ...
  exports {package} to {module_name}, {module_name}....
  ...
}

 

Example

module M1{
	...
	exports com.sample.app.util to M2, M3
	...
}

 

In the example, the package 'com.sample.app.util' is exposed to modules M2 and M3 only.

 

 

 

Previous                                                    Next                                                    Home

Module : Accessibility

As you see following module descriptor file, module ‘com.sample.xml’ depends on java.base module and exports the package ‘com.sample.xml.util’ to outside word.

 

module-info.java

module com.sample.xml{
   requires java.base;

   exports com.sample.xml.util;
}

 

Let’s define other module ‘com.sample.transformations’ which depends on the module ‘com.sample.xml’.

module com.sample.transformations{
   requires com.sample.xml;

   exports com.sample.transformations.util;
}

 

As you see ‘com.sample.transformations’ module depends on the module ‘com.sample.xml’, so it can access the types from the package ‘com.sample.xml.util’. But one catch here is that, not everything is exposed from the package ‘com.sample.xml.util’. Access specifiers still play a role in reading.

 

Only public types are accessible in other modules. For Non-public types, traditional access rules still apply.

 

Access Specifier

Class

Package

Subclass

Everyone

public

Y

Y

Y

Y

protected

Y

Y

Y

N

default

Y

Y

N

N

private

Y

N

N

N

 

Accessibility is not transitive

module M3{
	requires M2;
}

module M2{
	requires M1;

	export com.sample.doc.util;
}


module M1{
	export com.sample.text.util;
}

 

 


 

As you see above image, module M2 depends on M1 and M3 depends on M2. However, Module M3 cannot access types from exported package of Module M1 through its dependency on M2. If M3 want to access the types from M1 exported packages directly, then it should specify the dependency on M1 explicitly using ‘requires’ keyword.


 

Alternative way to handle transitive dependencies

Using ‘requires transitive’ we can handle transitive dependencies in much more better way.

 

 

 module M2{
	requires transitive M1;

	export com.sample.doc.util;
}


Since Module ‘M2’ define the dependent module M1 using ‘requires transitive’ keyword, if any module reads M2, it automatically reads M1 too. No need to explicitly define dependency on module M1.



Previous                                                    Next                                                    Home

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