views:

668

answers:

2

If I have a button like the one in this image :

http://www.freeimagehosting.net/image.php?4cd775814c.png

how could I make the text display itself vertically ? As in

j
B
u
t
t
o
n
1

I would like to know how to do the same thing for JLabel . I'm sure there has to be a better way than to create as many labels as there are characters in the string . Right ?

EDIT: how can I insert an image into my post ? The button for the image shows the image in the preview section , but when I actually post the data , I only get some text back , like the tags are getting messed up .

+4  A: 

You can use HTML for JButton or JLabel. So

button = new JButton("<html>J<br>b<br>u<br>t<br>t<br>o<br>n<br>1</html>");

should do the trick.

asalamon74
+5  A: 

I don't know if this is useful to you, but this sample shows how to make a vertical label. The difference is that it rotates all of the text (and/or the icon) instead of stacking the letters. I don't know how difficult it would be to modify it to do what you're asking.

The advantage of doing it this way is if your labels can change at runtime; generating big HTML strings might be a pain compared to a simple label.setUI(new VerticalLabelUI(true)); (and you don't have to worry about <> in the label text).

Michael Myers