1

I have a JLabel on the bottom of my panel that gives text instructions to the user. Some of the text went off the screen because it was too long. To fix this, I added tags. However, now the text is no longer centered, it is now aligned to the left. Why is this case. Shouldn't this code center the text?

detailedInstructions.setText("<html><div align=center>" + test.getMicroSteps()[currMicroStep].getDescription() + "</div><html>");
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
drake
  • 259
  • 4
  • 17

1 Answers1

2

Instead of using HTML code, simply call

detailedInstructions.setHorizontalAlignment(SwingConstants.CENTER);

Or use the JLabel constructor that sets the horizontal alignment.

Note that if the JLabel displays an ImageIcon, then you'll also want to set it's horizontal text position which determines the location of the label's text relative to its image.

Other potential issues is that the JLabel itself might not be centered at teh bottom of the GUI. To test this, put a border around the label to see it's actual placement, and if so, then you will need to work with the settings of the label's container's layout manager.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Will this address my issue of the text being too long though? I am able to center my text now, but without tags, I can't fit all the text onto the screen. – drake Jul 24 '15 at 16:44
  • @drake: To fully answer this, you'd want to create and post a minimal example program or mcve. Options to consider including changing the JLabel's font or not using a JLabel but rather a JTextArea that has been tricked out to be non-editable and to look like a JLabel. – Hovercraft Full Of Eels Jul 24 '15 at 16:55
  • I ended up using the method that you mentioned and moved the tags inside of the getDescription() method. – drake Jul 24 '15 at 16:59