Rotate JButton To Vertical


I had a previous post regarding rotating a JLabel and that involved creating a class that extends the BasicLabelUI class to do the rotation. JButton also has a class of its own called BasicButtonUI that you need to inherit in order to do rotation vertically.

To set the JButton to a certain angle, just set the parameter as either 90 or 270 in the VerticalButtonUI class. Kudos to the developer who created this class (I don’t know who).

1
jButton2.setUI(new VerticalButtonUI(90));

Here is the code for the VerticalButtonUI 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 VerticalButtonUI extends BasicButtonUI {
 
    protected int angle;
 
    public VerticalButtonUI(int angle) {
        super();
        this.angle = angle;
    }
 
    @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) {
        JButton button = (JButton)c;
        String text = button.getText();
        Icon icon = (button.isEnabled()) ? button.getIcon() : button.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;
 
        Graphics2D g2 = (Graphics2D) g;
        AffineTransform tr = g2.getTransform();
 
        if (angle == 90) {
            g2.rotate( Math.PI / 2 );
            g2.translate( 0, - c.getWidth() );
            paintViewR.x = c.getHeight()/2 - (int)fm.getStringBounds(text, g).getWidth()/2;
            paintViewR.y = c.getWidth()/2 - (int)fm.getStringBounds(text, g).getHeight()/2;
        }
        else if (angle == 270) {
            g2.rotate( - Math.PI / 2 );
            g2.translate( - c.getHeight(), 0 );
            paintViewR.x = c.getHeight()/2 - (int)fm.getStringBounds(text, g).getWidth()/2;
            paintViewR.y = c.getWidth()/2 - (int)fm.getStringBounds(text, g).getHeight()/2;
        }
 
        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 (button.isEnabled()) {
                paintText(g,c,new Rectangle(paintViewR.x,paintViewR.y,textX,textY),text);
            } else {
                paintText(g,c,new Rectangle(paintViewR.x,paintViewR.y,textX,textY),text);
            }
        }
 
        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

One Response to “Rotate JButton To Vertical”

  1. 1
    YGS LYS Says:

    Thanks,

    I will use these button :)

Leave a Reply

Spam protection by WP Captcha-Free