i need to make a vertical JLabel
- a JLabel
which shows it's text vertically- i searched google but i didn't find a good answer. how to do that?
Asked
Active
Viewed 1.2k times
4

Soheil
- 1,676
- 7
- 34
- 65
-
1please also see http://stackoverflow.com/questions/92781/how-do-i-present-text-vertically-in-a-jlabel-java-1-6 – Zhedar Feb 08 '13 at 17:21
-
@Zhedar the link which is said there has been expired and neither of answers work – Soheil Feb 08 '13 at 17:22
-
the 2nd answer (not accepted) gives another short explanation about the paint-approach. – Zhedar Feb 08 '13 at 17:23
-
@Zhedar but doesn't work...:( – Soheil Feb 08 '13 at 17:27
-
1I think [this](http://stackoverflow.com/a/6333584/584862) is exactly what you're looking for. – mre Feb 08 '13 at 19:42
2 Answers
5
You could use the class VerticalLabelUI created by a dev: http://tech.chitgoks.com/2009/11/13/rotate-jlabel-vertically/ (dead link now thanks MadMike for the archive link)
JLabel jl = new JLabel("TEST");
jl.setUI(new VerticalLabelUI(true));
The VerticalLabelUI class
public class VerticalLabelUI extends BasicLabelUI {
static {
labelUI = new VerticalLabelUI(false);
}
protected boolean clockwise;
public VerticalLabelUI(boolean clockwise) {
super();
this.clockwise = clockwise;
}
@Override
public Dimension getPreferredSize(JComponent c) {
Dimension dim = super.getPreferredSize(c);
return new Dimension( dim.height, dim.width );
}
private static Rectangle paintIconR = new Rectangle();
private static Rectangle paintTextR = new Rectangle();
private static Rectangle paintViewR = new Rectangle();
private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
@Override
public void paint(Graphics g, JComponent c) {
JLabel label = (JLabel)c;
String text = label.getText();
Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
if ((icon == null) && (text == null)) {
return;
}
FontMetrics fm = g.getFontMetrics();
paintViewInsets = c.getInsets(paintViewInsets);
paintViewR.x = paintViewInsets.left;
paintViewR.y = paintViewInsets.top;
// Use inverted height & width
paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
Graphics2D g2 = (Graphics2D) g;
AffineTransform tr = g2.getTransform();
if (clockwise) {
g2.rotate( Math.PI / 2 );
g2.translate( 0, - c.getWidth() );
} else {
g2.rotate( - Math.PI / 2 );
g2.translate( - c.getHeight(), 0 );
}
if (icon != null) {
icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
}
if (text != null) {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();
if (label.isEnabled()) {
paintEnabledText(label, g, clippedText, textX, textY);
} else {
paintDisabledText(label, g, clippedText, textX, textY);
}
}
g2.setTransform( tr );
}
}

eXa
- 618
- 8
- 18
-
1The linked webpage is gone. Fortunately there is the [web.archive.org](https://web.archive.org) which has a copy of it [here](https://web.archive.org/web/20190511215129/http://tech.chitgoks.com/2009/11/13/rotate-jlabel-vertically/) – MadMike Nov 09 '22 at 06:44
1
You can create a method that will transform your text into an HTML
code like this:
public static String transformStringToHtml(String strToTransform) {
String ans = "<html>";
String br = "<br>";
String[] lettersArr = strToTransform.split("");
for (String letter : lettersArr) {
ans += letter + br;
}
ans += "</html>";
return ans;
}
Afterwards, if you'll use this method in a setText
method like this: someLabel.setText(transformStringToHtml(someString));
where someString = "Test"
you will receive:
T
e
s
t
in your label.

Michael
- 1,209
- 2
- 12
- 25
-
-
1
-
I have not downvoted, but: the OP aksed for a rotated text, not for letters unrotation below each other. – AlexWien Feb 08 '13 at 18:00
-
@AlexWien I didn't accuse you for down voting. But you are right, the answer that was accepted answers the question correctly. Other than that, this solution is pretty quick and not complicated. – Michael Feb 08 '13 at 18:06
-
I event don't like the accepted answer, I would have done it with a custom view and an transformation matrix. Probaly this exaclty is what the external lib makes, but I avoid external libs if they havent a big benefit. – AlexWien Feb 08 '13 at 18:09
-
-
1