javaswingjlabeljavax.swing.text

jlabel icon right alignment and text left alignment


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.


Solution

  • 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:

    1. Use some container with label at left side and icon in a separate label at right side
    2. Create specific JLabel UI that will paint icon in the way you want