It is possible to create a jlabel with an icon so that the icon is aligned right ( right justified ) and the text is left justified. That means, in same label component i want the text to appear at the very left end of label and the icon at the far right.
Asked
Active
Viewed 1,696 times
0
-
3I prefer the solution of @trashgod in the above question, but you can also "play" with the [iconTextGap of the JLabel](http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setIconTextGap(int)), if you really want to have a single component. – Guillaume Polet Sep 12 '12 at 18:46
-
1Yet another alternative is suggested [here](http://stackoverflow.com/a/12251409/230513). Why not choose an approach and edit your question to include an [sscce](http://sscce.org/) that exhibits any problems you encounter. – trashgod Sep 13 '12 at 00:30
1 Answers
1
Standard JLabel cannot make a non-static spacing between icon and text, you can only specify iconTextGap in pixels, icon position relative to text and text alignment.
To make what you want you have a few options:
- Use some container with label at left side and icon in a separate label at right side
- Create specific JLabel UI that will paint icon in the way you want

Mikle Garin
- 10,083
- 37
- 59
-
+1 not sure about directions from your answer, [for example, I'm love not Container, not Component as JLabel, JViewport and GlassPane are](http://stackoverflow.com/questions/8575641/how-returns-xxxsize-from-jcomponents-added-to-the-jlabel) – mKorbel Sep 13 '12 at 10:09
-
@mKorbel erm, didn't really get what you wanted to say in your comment... well, atleast the link part of it – Mikle Garin Sep 13 '12 at 10:28