Jlabel text alignment not working when used with ICON

41 Views Asked by At

When using Icon in Jlabel the text is getting wrapped.

  1. I want to solve two scenarios, small text with a background Icon
  2. Bigger text (No wrap) with no ICON in background

How to align the text properly when the ICON is no longer required. Also It would be lot helpful if someone can throw some ideas on how to blink the background to create an animation effect.

As Expected

text getting wrapped up

expected (Without background)

package test.client.swing.machine.view;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import test.client.swing.machine.view.ImageFactory.BorderType;

import static test.client.swing.machine.view.ComponentNameConstants.LABEL_VALUE_BUTTON_LABEL;
import static test.client.swing.machine.view.ComponentNameConstants.LABEL_VALUE_BUTTON_VALUE;


public class LabelValueButtonDynamic extends SimpleGraphicButton {

    protected final JLabel left = new JLabel();
    protected JLabel right = new JLabel();
    protected String label;
    protected Object value;
    protected boolean showBackground = true;

    public LabelValueButtonDynamic(ImageFactory imageFactory, String label, Object value, Border border,
                            String imageNamePattern, BorderType bType, int width) {
        super(imageFactory);
        try {
            loadDynamicImages(imageNamePattern, bType, width);
        } catch (IllegalArgumentException e) {
            loadStandardImages(imageNamePattern);
        }
        setBorder(border);
        init(label, value); //NOSONAR needs to be overrideable
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
        left.setText(label);
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
        right.setText(String.valueOf(value));
    }

    @Override
    public void setForeground(Color c) {
        super.setForeground(c);
        if (left != null) {
            left.setForeground(c);
        }
        if (right != null) {
            right.setForeground(Color.RED);
        }
        // repaint anscheinend nicht nötig
        // repaint();
    }

    @Override
    public void setFont(Font f) {
        super.setFont(f);
        if (left != null) {
            left.setFont(f);
        }
        if (right != null) {
            right.setFont(f);
        }
        // repaint anscheinend nicht nötig
        //repaint();
    }

    @Override
    public Font getFont() {
        return left != null ? left.getFont() : super.getFont();
    }

    protected void init(String label, Object value) {

        showBackground = true;
        setLabel(label);
        setValue(value);
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        if (getComponentOrientation().isHorizontal()) {
            //          add(Box.createHorizontalStrut(5));
            add(left);
            add(Box.createGlue());
            add(right);
            //          add(Box.createHorizontalStrut(15));

            left.setAlignmentY(0.5f); // XXX optimalen Wert für AlignmentY finden (0.5 ist zu weit oben)
            right.setAlignmentY(0.5f);
            right.setIcon(new ImageIcon(loadStandardImage("btn_bonus_radio_button_unselected.png",
                    null)));
            right.setHorizontalAlignment(0);
            right.setHorizontalTextPosition(0);
        } else {
            //          add(Box.createVerticalStrut(5));
            add(left);
            add(Box.createVerticalGlue());
            add(right);
            //          add(Box.createVerticalStrut(15));
        }
        left.setFont(Constants.getFontBold());
        left.setName(LABEL_VALUE_BUTTON_LABEL);
        right.setFont(Constants.getFontBoldMedium());
        right.setName(LABEL_VALUE_BUTTON_VALUE);
        right.setPreferredSize(right.getPreferredSize());
    }

    public void showCircle(boolean show){
        if(show){
            right.setIcon(new ImageIcon(loadStandardImage("btn_bonus_radio_button_unselected.png",
                    null)));
            right.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            right.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        }
        else{
            right.setHorizontalAlignment(LEFT);
            right.setHorizontalTextPosition(LEFT);
            right.setIcon(null);
        }
        repaint();
    }
}

0

There are 0 best solutions below