javaswingjlabellayout-managerboxlayout

Multiline JLabel using HTML tags and alignment issues


I have Jlabels in JSsrollPane which are aligned to right. I want the Labels to be multi-lined, so I add <html>hello<br>world<html>. Now the problem is that the label no longer aligns to the right instead it fills the whole layout.

How it looks without html tags.

enter image description here

How it looks with html tags

enter image description here

MRE:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.WindowEvent;

public class Test extends JPanel {

    static JScrollPane scroll_area = new JScrollPane();
    static JPanel panel = new JPanel();
    static JButton btn = new JButton("Add");

    private static final Color lbl_color = new Color(171, 171, 171);
    private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10,10, 10);


    Test(){
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        scroll_area.getViewport().add(panel);
        scroll_area.createVerticalScrollBar();
        scroll_area.createHorizontalScrollBar();

        add(scroll_area);
        add(btn);

        btn.addActionListener(e -> addText());

        setSize(300, 30);

    }

    public static void addText(){
        String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth</font></html>";
//        String text = "Hello world Welcome to Earth";

        JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);

        mess_lbl.setOpaque(true);
        mess_lbl.setBackground(lbl_color);
        mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);

        panel.add(mess_lbl);
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
        panel.updateUI();
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
        frame.add(new Test());
        frame.setSize(500, 300);
        frame.setVisible(true);

    }

}

I would like my multi-lined labels to the right side of the layout as shown in image1.


Solution

  • I don't have much experience with BoxLayout, so you might want to wait and see if someone else has any ideas or suggestions. But with just a little bit of effort I was able to get it to work using a GridBagLayout instead

    Layout

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.WindowEvent;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.border.EmptyBorder;
    
    public class Test extends JPanel {
    
        static JScrollPane scroll_area = new JScrollPane();
        static JPanel panel = new JPanel();
        static JButton btn = new JButton("Add");
    
        private static final Color lbl_color = new Color(171, 171, 171);
        private static final EmptyBorder lbl_padding = new EmptyBorder(10, 10, 10, 10);
    
        private JPanel filler = new JPanel();
    
        Test() {
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    
            panel.setLayout(new GridBagLayout());
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(4, 0, 4, 0);
            gbc.weighty = 1;
    
            panel.add(filler, gbc);
    
            scroll_area.getViewport().add(panel);
            scroll_area.createVerticalScrollBar();
            scroll_area.createHorizontalScrollBar();
    
            add(scroll_area);
            add(btn);
    
            btn.addActionListener(e -> addText());
    
            setSize(300, 30);
    
        }
    
        private int counter;
    
        public void addText() {
            counter += 1;
            String text = "<html><font color='#F9F6F6'>Hello world <br> Welcome to Earth " + counter + "</font></html>";
    //        String text = "Hello world Welcome to Earth";
    
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.EAST;
            gbc.weightx = 1.0;
            gbc.insets = new Insets(4, 0, 4, 0);
    
            JLabel mess_lbl = new JLabel(text, JLabel.RIGHT);
    
            mess_lbl.setOpaque(true);
            mess_lbl.setBackground(lbl_color);
            mess_lbl.setAlignmentX(Component.RIGHT_ALIGNMENT);
    
            panel.add(mess_lbl, gbc, panel.getComponentCount() - 1);
    
            //panel.add(Box.createRigidArea(new Dimension(0, 5)));
            panel.revalidate();
            panel.repaint();
        }
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame();
            frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
            frame.add(new Test());
            frame.setSize(500, 300);
            frame.setVisible(true);
    
        }
    
    }