Link to home
Start Free TrialLog in
Avatar of tedschnieders
tedschnieders

asked on

adding JLabel right justified

I am adding a JLabel to a  container with a border layout and i need to know how to make it right justified or where i would look at java.sun.com to find the answer.


thanks much
Ted Schnieders
Avatar of Mick Barry
Mick Barry
Flag of Australia image

You can also specify alignment in JLabel constructor
Hi Ted Schnieders,

Do you want it to be right justified in the label itself or do you want it to be right justified in the BorderLayout?

Regard
Dave
You can do it like this :

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author Javatm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Sample extends JFrame {

      public Sample() {
            
            super("Sample . . .");
            
            JLabel label1 = new JLabel();
            label1.setText("Right Side");
            
            getContentPane().add(label1, BorderLayout.EAST);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(400, 300);
            setVisible(true);
      }
      public static void main(String[] args) {
            Sample x = new Sample();
      }
}
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There's also a sample here which does lots of alignment and stuff: http://www.java2s.com/ExampleCode/Swing-JFC/Labelalignment.htm
> BTW, Javatm, welcome back :)

Yap, I was prett busy w/ my new work, kind super bc this past few months. I can see that your already a wizard.
May the force be w/ you I'll try to notch few more points for that, thanks.
Good luck :)