Friday, November 3, 2017

JLabel: setHorizontalTextPosition vs setHorizontalAlignment

A JLabel can display a text string or an image or both.

The setHorizontalTextPosition(int textPosition) method is used when a JLabel displays both an image and a text. This method specifies whether the text should be displayed on the left or right side or center of the image. It takes an argument which is one of these SwingConstants values: LEFT, CENTER, RIGHT, LEADING or TRAILING. TRAILING is the default position.

The setHorizontalAlignment(int alignment) method is used to specify the alignment of the whole contents on the JLable. It also takes an argument which is one of these SwingConstants values: LEFT, CENTER, RIGHT, LEADING or TRAILING.

For example, you have a JLabel which only displays the text "User Name", and you want the text to be at the right side of the label. You have two ways to achieve this.

1. JLabel userLabel = new JLabel ("User Name", SwingConstants.RIGHT");

2. JLabel userLabel = new JLabel();

      userLabel.setText("User Name"); //by default, JLabel displays text at the left edge.
      userLabel.setHorizontalAlignment(SwingConstants.RIGHT);

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment