Saturday, 12 May 2018

JavaFX: Label: Wrapping up the text

By using setWrapText() method, you can place the text within a space that is smaller than the text length.

Ex
Label label1 = new Label("You can learn java programming at 'https://self-learning-java-tutorial.blogspot.com'");
label1.setWrapText(true);
label1.setFont(new Font("Arial", 30));
label1.setTextAlignment(TextAlignment.RIGHT);

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class LabelApp extends Application {
 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {

  Label label1 = new Label("You can learn java programming at 'https://self-learning-java-tutorial.blogspot.com'");
  label1.setWrapText(true);
  label1.setFont(new Font("Arial", 30));
  label1.setTextAlignment(TextAlignment.RIGHT);

  VBox vBox = new VBox(10, label1);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 400, 500);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}




Previous                                                 Next                                                 Home

JavaFX: Label: setFont: Set label font

By using ‘setFont’ method, you can set the font to the label.

Ex
Label label1 = new Label("Hello World");
label1.setFont(new Font("Arial", 30));

Label label2 = new Label("Hello World");
label2.setFont(new Font("Cambria", 30));

Find the below working application.

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class LabelApp extends Application {
 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {

  Label label1 = new Label("Hello World");
  label1.setFont(new Font("Arial", 30));

  Label label2 = new Label("Hello World");
  label2.setFont(new Font("Cambria", 30));

  VBox vBox = new VBox(10, label1, label2);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 500, 500);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}





Previous                                                 Next                                                 Home

Thursday, 10 May 2018

JavaFX: Label: setContentDisplay: Set the position of graphic relative to text

By using ‘setContentDisplay’ method, you can set the position of graphic relative to the text.

Signature of 'setContentDisplay' method
public final void setContentDisplay(ContentDisplay value)

ContentDisplay is an enum, it represents below constants.

ContentDisplay constant
Description
TOP
Content will be placed at the top of the Label.
RIGHT
Content will be placed at the right of the Label.
BOTTOM
Content will be placed at the bottom of the Label.
LEFT
Content will be placed at the left of the Label.
CENTER
Content will be placed at the center of the Label.
GRAPHIC_ONLY
Only the content will be displayed.
TEXT_ONLY
Only the label's text will be displayed.

Ex
Label label1 = new Label("label1", new ImageView(image));
label1.setContentDisplay(ContentDisplay.BOTTOM);

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LabelApp extends Application {
 private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\search.png";

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {
  InputStream is = new FileInputStream(imageFilePath);
  Image image = new Image(is);

  Label label1 = new Label("label1", new ImageView(image));
  label1.setContentDisplay(ContentDisplay.BOTTOM);

  Label label2 = new Label("label2", new ImageView(image));
  label2.setContentDisplay(ContentDisplay.CENTER);

  Label label3 = new Label("label3", new ImageView(image));
  label3.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);

  Label label4 = new Label("label4", new ImageView(image));
  label4.setContentDisplay(ContentDisplay.LEFT);

  Label label5 = new Label("label5", new ImageView(image));
  label5.setContentDisplay(ContentDisplay.RIGHT);

  Label label6 = new Label("label6", new ImageView(image));
  label6.setContentDisplay(ContentDisplay.TEXT_ONLY);

  Label label7 = new Label("label7", new ImageView(image));
  label7.setContentDisplay(ContentDisplay.TOP);

  VBox vBox = new VBox(10, label1, label2, label3, label4, label5, label6, label7);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 600, 800);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}



Previous                                                 Next                                                 Home

JavaFX: Label: Align label text

By using 'setTextAlignment' method, you can change the position of the label content within its layout area.

Signature of 'setTextAlignment' method
public final void setTextAlignment(TextAlignment value)

TestAlignment is an enum type with below values.

Alignment value
Description
LEFT
Represents text alignment to the left
CENTER
Represents centered text alignment
RIGHT
Represents text alignment to the right
JUSTIFY
Represents justified text alignment.

Ex
Label label1 = new Label("Hello World");
label1.setTextAlignment(TextAlignment.JUSTIFY);
label1.setWrapText(true);

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class LabelApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {
  Label label1 = new Label("Hello World");
  label1.setTextAlignment(TextAlignment.JUSTIFY);
  label1.setWrapText(true);
 
  VBox vBox = new VBox(10, label1);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 200, 200);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}




Previous                                                 Next                                                 Home

Can a private constructor prevent sub-classing of class?

Inheritance is the concept of re usability. Object of one class can get the properties and methods of object of another class by using inheritance. One class can use the methods and properties of other class.

When you want to create a new class and there is already another class that has some of the functionality that you want, you can derive your new class from the existing class.


Class B derived from class A. I.e., B is the sub class and A is the super class.

   Class A{

   }

   class B extends A{

   }

Whenever sub class constructor is called, the super class default constructor called internally by default.

Can a private constructor prevent a class from sub-classing?
A private constructor does not prevent a class from sub-classing.

For example,

Application.java
package com.sample.app;

public class Application {
 private static class ParentClass {
  private ParentClass() {
   System.out.println("I am in parent class");
  }
 }

 private static class ChildClass extends ParentClass {
  public ChildClass() {
   System.out.println("I am in child class");
  }
 }

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


Output
I am in parent class
I am in child class



Wednesday, 9 May 2018

JavaFX: Label: Set gap between text and graphic image

You can use 'setGraphicTextGap' method to set the gap between the label text and graphical icon associated with it.

Ex
InputStream is = new FileInputStream(imageFilePath);
Image image = new Image(is);
Label label = new Label("Java Icon", new ImageView(image));
label.setGraphicTextGap(100);

Find the below working application.

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LabelApp extends Application {

 private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\javaIcon.png";

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {
  InputStream is = new FileInputStream(imageFilePath);
  Image image = new Image(is);
  Label label = new Label("Java Icon", new ImageView(image));
  label.setGraphicTextGap(100);

  VBox vBox = new VBox(10, label);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 600, 600);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}





Previous                                                 Next                                                 Home

JavaFX: Set label text color

By using ‘setTextFill’ method of Label class, you can set the text color.

Ex
Label label1 = new Label();
label1.setText("Hello JavaFX");
label1.setTextFill(Color.web("#ff0000", 0.8));

Find the below working application.

LabelApp.java
package com.sample.demos;

import static javafx.geometry.Pos.CENTER;

import java.io.FileNotFoundException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class LabelApp extends Application {


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

  Label label1 = new Label();
  label1.setText("Hello JavaFX");
  label1.setTextFill(Color.web("#ff0000", 0.8));

  VBox vBox = new VBox(10, label1);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 200, 200);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Label Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(LabelApp.class, args);
 }
}





Previous                                                 Next                                                 Home