Search code examples
javaswingjlabel

JLabel not aligned to the left


My JLabel hudHeader isn't aligned to left.

Here is what it looks like at the moment: enter image description here

HUDisplay extends JPanel.

public HUDisplay() {

Border border = BorderFactory.createMatteBorder(0, 1, 0, 0, new Color(128, 128, 128));
Border margin = new EmptyBorder(8,8,8,8);
setBorder(new CompoundBorder(border, margin));
setBackground(new Color(250,250,250));

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


hudHeader = new JLabel("<html><b>Simulation informations</b><br />" +
        "<i style=\"font-size: 8px;\">Running for " + "0" + "s</i>" + "</html>");

hSeparation = new JSeparator(SwingConstants.HORIZONTAL);

add(hudHeader);
add(hSeparation);

setVisible(true);

}

Solution

  • NOT SO: It looks like an HTML body margin

    NOT hudHeader = new JLabel(
    NOT         "<html><body style='margin: 0 0 0 0;'><b>Simulation informations</b><br />" +
    NOT         "<i style=\"font-size: 8px;\">Running for " + "0" + "s</i>" + "</html>");
    

    However there might be something fishy, like RTL, so try too

    hudHeader .setAlignment(SwingConstants.LEFT);