Rotate JLabel Vertically


To rotate a JLabel‘s text vertically, you only need to use this class and pass it as a parameter to the JLabel‘s setUI() method. Kudos to the developer who created this class to make it easy for other developers to plug this class in and create a vertical JLabel in an instant.

To use this class, check the code below:

1
2
JLabel jl = new JLabel("TEST");
jl.setUI(new VerticalLabelUI(true));

This is the code for the VerticalLabelUI class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 );
    }
 
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

2 Responses to “Rotate JLabel Vertically”

  1. 1
    Pavel Pěnkava Says:

    Thank you very much. Finally I found a working one class ;-)
    BTW: Maybe it would be useful to post imported packages and fix &amps in the code :-)

  2. 2
    tech Says:

    lolz. if you use a GUI, you can automatically import classes. just did not want to post them as it would lengthen the code ;)

    but thanks

Leave a Reply

Spam protection by WP Captcha-Free