Monday 14 May 2018

JavaFX: Label: Render big strings in limited space

In my previous post, I explained how to wrap the text. But there are some situations, where the space for rendering the text is smaller than the space needed to render the entire string. In these scenarios, you can use 'setTextOverrun' and specify how to process the remaining string that cannot be rendered properly.

Signature of setTextOverrun method
public final void setTextOverrun(OverrunStyle value)

'OverrunStyle' is an enum provides below constnats to define the wrapping behavior.
Constant
Description
CLIP
Any text which exceeds the bounds of the label will be clipped.
ELLIPSIS
If the text of the label exceeds the bounds of the label, then the last three characters which can be displayed will be "...".
WORD_ELLIPSIS
Same as ELLIPSIS, but first removes any partial words at the label boundary and then applies the ellipsis.
CENTER_ELLIPSIS
Trims out the center of the string being displayed and replaces the middle three characters with "...". The first and last characters of the string are always displayed in the label, unless the label becomes so short that it cannot display anything other than the ellipsis.
CENTER_WORD_ELLIPSIS
Same as CENTER_ELLIPSIS but ensures that the "..." occurs between full words. If the label becomes so short that it is not possible to trim any additional words, then partial words are displayed and this behaves the same as CENTER_ELLIPSIS.
LEADING_ELLIPSIS
Same as ELLIPSIS but puts the "..." at the beginning of the text instead of at the end.
LEADING_WORD_ELLIPSIS
Same as WORD_ELLIPSIS but puts the "..." at the beginning of the text instead of at the end.

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);
label1.setTextOverrun(OverrunStyle.ELLIPSIS);

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.control.OverrunStyle;
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);
  label1.setTextOverrun(OverrunStyle.ELLIPSIS);

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

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

  /* 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 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.control.OverrunStyle;
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);
  label1.setTextOverrun(OverrunStyle.ELLIPSIS);

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

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

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

}





Previous                                                 Next                                                 Home

No comments:

Post a Comment