Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

JLabel: how to get space between two span tags

Have following program

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class HTMLabelTryout {

    private static final String TEXT =
            "<html><span style=\"font-size:24px; margin-right:16px; color:#007f9c\">This is a paragraph.</span>"
                    + "<span style=\"font-size:16px\">This is another paragraph.</span></html>";

    private static final String TEXT2 =
            "<html><p style=\"font-size:24px; margin-bottom:16px; color:#007f9c\">This is a paragraph.</p>"
                    + "<p style=\"font-size:16px;\">This is another paragraph.</p></html>";

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new HTMLabelTryout()::showFrame);
    }

    private void showFrame() {
        JFrame frm = new JFrame();
        frm.add(new JLabel(TEXT), BorderLayout.PAGE_START);
        frm.add(new JLabel(TEXT2), BorderLayout.PAGE_END);
        frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frm.setSize(900, 400);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

Second text looks OK, but in first I get no space between two spans (margin-right/margin-left instructions are ignored). Any ideas, how to get 16px horizontal gap between two spans in Swing?

P.S. I know, I can add some non-breaking space tags between spans to get a space, but it's not exact and looks ugly.

P.P.S. For some reasons I must use HTML here and cannot switch to a JPanel with 2 JLabel solution.

Upvotes: 0

Views: 37

Answers (0)

Related Questions