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");
The default rotation is anticlockwise (vertical up). To rotate the label clockwise (vertical down) pass a parameter of true to the constructor.
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.