Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right-justify icon in a JLabel?

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.

like image 976
Geoffrey Zheng Avatar asked May 28 '10 20:05

Geoffrey Zheng


People also ask

How will you set icon for the JLabel?

To create a JLabel with an image icon we can either pass an ImageIcon as a second parameter to the JLabel constructor or use the JLabel. setIcon() method to set the icon.

Can a JLabel display an image?

JLabel can display text, image or both .

Can JLabel be editable?

It is impossible to make an editable label as it exists. You can use a jTextField with no border and same background as the JFrame. Having said that, you can add a KeyListener to your JLabel and update your label's text depending on the key that was pressed.


1 Answers

Is this the desired effect?

Addendum: I think a panel is the way to go.

image

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

public class TestJLabelIcon {

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setLayout(new GridLayout(0, 1));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(createPanel("abc"));
                frame.add(createPanel("defghij"));
                frame.add(createPanel("klmn"));
                frame.add(createPanel("opq"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            private JPanel createPanel(String s) {
                JPanel p = new JPanel(new BorderLayout());
                p.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST);
                Icon icon = UIManager.getIcon("FileChooser.detailsViewIcon");
                p.add(new JLabel(icon, JLabel.RIGHT), BorderLayout.EAST);
                p.setBorder(BorderFactory.createLineBorder(Color.blue));
                return p;
            }
        });
    }
}
like image 159
trashgod Avatar answered Oct 10 '22 14:10

trashgod