Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text and icon differently in JLabel

i'm trying to create a JLabel that has text aligned left and icon aligned right, i tried this code:

_ip = new JLabel(ip);
_ip.setFont(boldFont);
_ip.setBounds(5, 0, 100, 50);
_ip.setIcon(images.ipBan);
_ip.setBorder(BorderFactory.createLineBorder(Color.black, 1));
_ip.setHorizontalTextPosition(SwingConstants.LEFT);
add(_ip);

And this is what i get:

Alignment preview

The red image shows the actual image alignment, the gray one shows where i want my image to be.

If i add

_ip.setAlignmentX(JLabel.RIGHT_ALIGNMENT);

Nothing happens, and if i add

_ip.setHorizontalAlignment(JLabel.RIGHT);

Icon is aligned right, but also text is aligned right, and i want it to align left

Is there a way to do it?

like image 267
BackSlash Avatar asked Mar 31 '13 15:03

BackSlash


2 Answers

Alternatively, you can use a JPanel with a suitable layout, as shown here.

image

like image 79
trashgod Avatar answered Oct 20 '22 10:10

trashgod


please DYM???

enter image description here

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

public class CenteredJLabel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel("CenteredJLabel");
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");

    public CenteredJLabel() {
        panel.setLayout(new GridBagLayout());
        panel.add(label);
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setVerticalAlignment(SwingConstants.CENTER);
        label.setIcon(errorIcon);
        label.setIconTextGap(20);
        label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                CenteredJLabel centeredJLabel = new CenteredJLabel();
            }
        });
    }
}

EDIT

enter image description here

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

public class CenteredJLabel {

    private JFrame frame = new JFrame("Test");
    private JPanel panel = new JPanel();
    private JLabel labelOne = new JLabel("CenteredJLabel");
    private JLabel labelTwo = new JLabel("1.1.1.1");
    private JLabel labelThree = new JLabel("192.168.255.255");
    private JLabel labelFour = new JLabel("192.168.255.255");
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private Icon questnIcon = UIManager.getIcon("OptionPane.questionIcon");
    private JPanel panelTwo = new JPanel();

    public CenteredJLabel() {
        panel.setLayout(new GridLayout(4, 0, 10, 10));

        labelOne.setHorizontalAlignment(SwingConstants.LEFT);
        labelOne.setVerticalAlignment(SwingConstants.CENTER);
        labelOne.setIcon(errorIcon);
        labelOne.setIconTextGap(20);
        labelOne.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelOne);

        labelTwo.setHorizontalAlignment(SwingConstants.LEFT);
        labelTwo.setVerticalAlignment(SwingConstants.CENTER);
        labelTwo.setIcon(infoIcon);
        labelTwo.setIconTextGap(20);
        labelTwo.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelTwo);

        labelThree.setHorizontalAlignment(SwingConstants.LEFT);
        labelThree.setVerticalAlignment(SwingConstants.CENTER);
        labelThree.setIcon(warnIcon);
        labelThree.setIconTextGap(20);
        labelThree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.add(labelThree);

        panelTwo.setLayout(new BorderLayout());
        panelTwo.setOpaque(false);
        panelTwo.add(labelFour, BorderLayout.WEST);
        panelTwo.add(new JLabel(questnIcon), BorderLayout.EAST);
        panel.add(panelTwo);

        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CenteredJLabel centeredJLabel = new CenteredJLabel();
            }
        });
    }
}
like image 44
mKorbel Avatar answered Oct 20 '22 10:10

mKorbel