How to right-justify icon in a JLabel?

Posted by geoffreyzheng on Stack Overflow See other posts from Stack Overflow or by geoffreyzheng
Published on 2010-05-28T20:42:57Z Indexed on 2010/05/29 0:32 UTC
Read the original article Hit count: 617

Filed under:
|

For a JLabel with icon, if you setHorizontalTextPosition(SwingConstants.LEADING), the icon is painted right after text, no matter how wide the label is.

This is particularly bad for a list, as the icons would be all over the place depending on how long the text is for each item.

I traced the code and it seems to be that in SwingUtilities#layoutCompoundLabelImpl, text width is simply set to SwingUtilities2.stringWidth(c, fm, text), and icon x is set to follow text without considering label width.

Here is the simplest case:

import java.awt.*;
import javax.swing.*;

public class TestJLabelIcon
{
    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JLabel c = new JLabel("abc");
                c.setHorizontalTextPosition(SwingConstants.LEADING);
                c.setHorizontalAlignment(SwingConstants.LEADING);
                c.setIcon(UIManager.getIcon("FileChooser.detailsViewIcon"));
                c.setBorder(BorderFactory.createLineBorder(Color.RED));

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.getContentPane().add(c);    
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

You can see that label always fills the frame but icon stays put. You'll get the mirror problem if you set both arguments to TRAILING.

I know I can override the UI, or use a JPanel, etc. I just wonder if I'm missing something simple in JLabel. If not, it seems like a Java bug.

FYI this is jdk1.6.0_06 on Windows XP.

© Stack Overflow or respective owner

Related posts about swing

Related posts about jlabel