Vertical Label UI
Posted by Darryl Burke on February 21, 2009
Once in a way, one comes across a situation where the ability to render text vertically – rotated 90° – could both conserve screen real estate as well as make for a more aesthetic GUI.
VerticalLabelUI, which extends BasicLabelUI, provides a comprehensive solution to such need.
To the maximum extent possible, VerticalLabelUI applies suitable transforms and invokes the methods of its superclass for laying out and painting the label. As a result, changing the class to extend MetalLabelUI instead of BasicLabelUI will result in rendering a vertical label in the style of the Metal L&F, no other changes being needed. While not exhaustively tested, the same should be true of any custom L&F that uses a LabelUI which inherits from BasicLabelUI.
To use the class, simply invoke setUI on a JLabel:
JLabel label = new JLabel("Label");
label.setUI(new VerticalLabelUI());
The default rotation is anticlockwise (vertical up). To rotate the label clockwise (vertical down) pass a parameter of true to the constructor.
label.setUI(new VerticalLabelUI(true));
Possible uses for VerticalLabelUI include a rotated rowHeaderView for a JScrollPane and in a header renderer for a table which has small column content but long column names. The latter involves using a customized TableHeaderUI and renderer, which will be published in a future posting.
Get The Code
See Also
Vertical Table Header Cell Renderer

Shaun said
This appears to fail when you center a label in a panel.
When not rotated (label.setUI(new VerticalLabelUI()); commented out) it does indeed center vertically. However, as soon as we rotate (removing the comments) the text is pushed to the bottom of the panel :-(
Darryl Burke said
Well, that’s to be expected with the label’s default horizontal alignment, which is LEFT. If you want the text at the top of the rotated label, use
label.setHorizontalAlignment(JLabel.RIGHT);cheers, db
vaielet said
Hello!
I would like to use html tags in my vertical JLabel. However, if I rotate my JLabel using VerticalLabelUI, I can’t use html tags anymore. Any idea?
Thanks a lot.
Darryl Burke said
Sorry, I can’t see where you have a problem. Try this:
import darrylbu.plaf.vertical.VerticalLabelUI;import javax.swing.*;
public class VerticalLabelTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VerticalLabelTest().makeUI();
}
});
}
public void makeUI() {
String text = "This is a
vertical Label
with HTML";
JLabel label = new JLabel(text);
label.setUI(new VerticalLabelUI());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
cheers, db
Darryl Burke said
Oops, WordPress chewed up the code formatting and the HTML. Here’s another attempt.
import darrylbu.plaf.vertical.VerticalLabelUI;
import javax.swing.*;
public class VerticalLabelTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new VerticalLabelTest().makeUI();
}
});
}
public void makeUI() {
String text = "<HTML><CENTER>This is a<BR>vertical Label<BR>with HTML</CENTER></HTML>";
JLabel label = new JLabel(text);
label.setUI(new VerticalLabelUI());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
db
Anonymous said
Exactly what I need. Thank you very much, Darryl. Your code is used and quoted.
Anonymous said
Thanks for your code. I am able to use it on a table for vertical column header.
Matthew Wise said
Awesome cheers Darryl. It wasn’t working very well initially because the label bounds were exactly square and caused components to the right of the label to shift and become obscured. But I then realised that it was because I am using JGoodies FormLayout. If you use this layout manager, make sure you set the column size to “preferred” not “default”. HTH.
Darryl Burke said
Thank you, Matthew for sharing your experience.
Darryl
Fabio Daros (@Fabio_Daros) said
The text in JLabel not appear whole. Shows for example: 222 (222.34)…….. or …….. Hous (House)
Darryl Burke said
You wouldn’t be setting the prefferedSize, would you?
Fabio Daros (@Fabio_Daros) said
JLabel’s 100×30 and text 50×30
Darryl Burke said
Don’t do that.