1

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);

}
Al0x
  • 917
  • 2
  • 13
  • 30
  • https://stackoverflow.com/questions/9212155/java-boxlayout-panels-alignment as BoxLayout centers – Joop Eggen Nov 24 '17 at 11:46
  • I already tried, nothing changes with the following lines :-( setMaximumSize( new Dimension( 200, 600) ); setAlignmentX(Component.LEFT_ALIGNMENT); –  Nov 24 '17 at 11:51
  • It is mere adding JPanels. That will become stretched over the entire width, and then one can add a label left aligned in the panel. – Joop Eggen Nov 24 '17 at 11:52
  • That looks like `` which actually was my first impulse. – Joop Eggen Nov 24 '17 at 11:54
  • When I setMaximumSize and setAlignementX on HUDisplay (JPanel), the JLabel is still aligned to the "center". I don't know if the body's style property is a suggestion but it doesn't work aswell :-( –  Nov 24 '17 at 11:58
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 24 '17 at 12:10

1 Answers1

1

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);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • It works with the second option. I was using Component.LEFT_ALIGNEMENT instead of SwingConstants.LEFT. Thanks for your help. –  Nov 24 '17 at 12:12