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
thanks much
Ted Schnieders
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
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(label 1, BorderLayout.EAST);
setDefaultCloseOperation(E XIT_ON_CLO SE);
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args) {
Sample x = new Sample();
}
}
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(label
setDefaultCloseOperation(E
setSize(400, 300);
setVisible(true);
}
public static void main(String[] args) {
Sample x = new Sample();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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 :)
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JLabel.html#setHorizontalAlignment(int)