You have to use a fixed width font (
Monospaced font - Wikipedia[
^]) like
Courier to show properly aligned text tables. How to select such depends on the used output method.
This uses a fixed width font:
Item Price
AnItem € 13.49
Another € 127.80
The same using a proportional font:
Item Price
AnItem € 13.49
Another € 127.80
[EDIT]
Overlooked the
JList
.
To set the font use something like
myList.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
To format the line strings use
String.format
as suggested in solution 1:
String strLine = String.format("%-49s% 10.2f € ", nameBoisson, prixDevente);
Here
%-49s
prints a string left aligned with trailing spaces up to a total length of 49 characters and
% 10.2f
prints a floating point value right aligned with a total length of 10 characters, 2 digits after the decimal point, and a leading space for positive values.
[/EDIT]