4

i need to make a vertical JLabel- a JLabel which shows it's text vertically- i searched google but i didn't find a good answer. how to do that?

enter image description here

Soheil
  • 1,660
  • 7
  • 33
  • 63

2 Answers2

5

You could use the class VerticalLabelUI created by a dev: http://tech.chitgoks.com/2009/11/13/rotate-jlabel-vertically/

eXa
  • 529
  • 6
  • 17
2

You can create a method that will transform your text into an HTML code like this:

public static String transformStringToHtml(String strToTransform) {
    String ans = "<html>";
    String br = "<br>";
    String[] lettersArr = strToTransform.split("");
    for (String letter : lettersArr) {
        ans += letter + br;
    }
    ans += "</html>";
    return ans;
}

Afterwards, if you'll use this method in a setText method like this: someLabel.setText(transformStringToHtml(someString)); where someString = "Test" you will receive:

T
e
s
t

in your label.

Michael
  • 1,167
  • 2
  • 11
  • 24
  • This isn't what OP is looking for. – mre Feb 08 '13 at 17:57
  • 1
    @AlexWien Care to explain why? – Michael Feb 08 '13 at 17:57
  • I have not downvoted, but: the OP aksed for a rotated text, not for letters unrotation below each other. – AlexWien Feb 08 '13 at 18:00
  • @AlexWien I didn't accuse you for down voting. But you are right, the answer that was accepted answers the question correctly. Other than that, this solution is pretty quick and not complicated. – Michael Feb 08 '13 at 18:06
  • I event don't like the accepted answer, I would have done it with a custom view and an transformation matrix. Probaly this exaclty is what the external lib makes, but I avoid external libs if they havent a big benefit. – AlexWien Feb 08 '13 at 18:09
  • @AlexWien so why don't you share us your idea to solve this problem ? – Soheil Feb 08 '13 at 18:30
  • 1
    @Michael -1 not related to what i asked... – Soheil Feb 08 '13 at 18:31