Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put the text of a JLabel under its icon

I trying to put the text of a JLabel under its icon (centered) at the momemnt I can get this

Current

using JLabel north = new JLabel("North")

I tried using html in the label JLabel("<HTML><BR>North</HTML>") but it is not aligned properly. Any suggestions?


like image 940
Bilal Syed Hussain Avatar asked Mar 01 '12 01:03

Bilal Syed Hussain


People also ask

How do you put a JLabel under another JLabel?

The short answer is yes, as a JLabel is a Container , so it can accept a Component (a JLabel is a subclass of Component ) to add into the JLabel by using the add method: JLabel outsideLabel = new JLabel("Hello"); JLabel insideLabel = new JLabel("World"); outsideLabel.


1 Answers

See this:

  • http://www.java2s.com/Code/Java/Swing-JFC/LabelTextPosition.htm

Basically, use this in your case:

north.setHorizontalTextPosition(JLabel.CENTER);
north.setVerticalTextPosition(JLabel.BOTTOM);
like image 110
icyrock.com Avatar answered Oct 05 '22 15:10

icyrock.com