Home Java Java-tips GUI Components 10labels JLabel text alignment

Ask Questions?

View Latest Questions



JLabel text alignment
Posted on: July 26, 2006 at 12:00 AM
What tabs don't work?

Java: JLabel text alignment

What tabs don't work

The basic problem is that there is no fixed definition for tab, which more or less means move to the next tab position for the particular output device. It's mostly a historical feature that had a real hardware meaning (little metal "tabs" were inserted to stop the carriage).

In MS Word you can also move the tabs wherever you want. I use tab setting of 4 in the editor I use most frequently, but it used to be set a 3. Anyway, they surely didn't want to get into the mess of picking a given amount.

Why spaces don't work

And it's also impossible to align things using spaces with JOptionPane's variable pitch font.

One solution: HTML tables

One solution is to use HTML, a subset of which is allowed in JOptionPane. For example, to solve your problem.

  + "<tr><td>" + name + "</td><td>" + pieces + "</td><td>" + pay + "</td></tr>"
  + "</table></html>");

Of course, this requires knowing HTML, a good idea anyway. :-)

Solution: String.format(...)

Another solution is to use the String.format(...) method that's in recent versions of Java.

Copyleft 2005 Fred Swartz MIT License