Get Horizontal Alignment of JLabel - Java Swing

Java examples for Swing:JLabel

Introduction

To get horizontal alignment of label's content use, int getHorizontalAlignment() method of JLabel's class.

Return value is one of the following constants, LEFT, CENTER, RIGHT, LEADING or TRAILING.

Demo Code

import javax.swing.JLabel;

public class Main {

  public void init() {

    JLabel label1 = new JLabel("JLabel Horizontal Alignment.");

    int alignment = label1.getHorizontalAlignment();

    switch (alignment) {

    case JLabel.LEFT:
      label1.setText("Left");
      break;//w ww . j  a  v  a  2s  .com
    case JLabel.CENTER:
      label1.setText("Center");
      break;
    case JLabel.RIGHT:
      label1.setText("Right");
      break;
    case JLabel.LEADING:
      label1.setText("Leading");
      break;
    case JLabel.TRAILING:
      label1.setText("Trailing");
      break;
    }
  }
}

Related Tutorials