Danine
Danine

Reputation: 3

How add margin html inside a JLabel component

I've tried add margin left in a text inside JLabel, but not works.

Can you help me?

I tried add div with style, but no result.

Code JLabel Text:

"<html>" + ((JLabel) component).getText() + 
"<font style=color:blue;float: left; width=200> > </font>" +
"<font style=color:green> ..." + file.getName() + "</font>" +
"</html>"

I want that the first part of Jlabel must be left, and the second part of JLabel add on right.

Actual (Example1) Expected (Example2) Actual/Expected

Upvotes: 0

Views: 299

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

Use table tag to provide margin.

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

/**
 * <code>TableInLabel</code>.
 */
public class TableInLabel {

    private static final String LEFT = "left part";

    private static final String RIGHT = "right part";

    public static void main(String[] args) {
        String result =
                "<html><table><td width=\"200\"><font style=color:blue>"
                        + LEFT
                        + "</font></td><td>"
                        + RIGHT + "</td></table></html>";
        JLabel lbl = new JLabel(result);
        JFrame frm = new JFrame("Label");
        frm.add(lbl);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}

Upvotes: 2

Related Questions