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).

Here is the code for the VerticalButtonUI class.

Related Posts Plugin for WordPress, Blogger...

16 comments

  1. Nice code works well –
    if ((icon == null) && (text == null))
    should be-
    if ((icon == null) && (text == null))

    JD

  2. I can make a vertical button easy, the problem is getting the text to appear vertically within. Html tagging creates gaps between the letters unless the button is wide enough to accommodate (even though the tagging is invisible to the eye). My most imperfect solution is to create a graphic of the text vertically and use that as the button icon.

  3. It’s really quite simple, but it doesn’t look pretty. Yet.

    I used Photoshop Elements.

    I type in text mode my first letter and hit enter. Then the next letter and enter again. etc…

    Then used the circle select tool to select only the text in an oval.

    Saved that as a small JPG, and used that as the icon.

    I used the same font, but it is thinner than the text in my other buttons. It needs a little work.

    I’m surprised and a little annoyed that this is such a task.

  4. I second Richard’s comment… the button background appears disabled. Will work on this and see if I can get it fixed.

  5. Very helpful. Thanks to the contributor(s). The cast to JButton on line 23 can and probably should be changed to AbstractButton to be as generic as possible. I had to do this, since my button was a JToggleButton.

Leave a Reply

Your email address will not be published. Required fields are marked *