Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right Justification in Java

Tags:

java

I've tried looking but I can't seem to find a solution to my justification problem. I want all the transaction amounts, which turn up with 2 decimals digits, to be all justified to the right, however, nothing I try seems to work. This is the result:

Java dialog with unaligned numbers

    Transaction temp;
    String message = "";
    for (int i = 0; i < checkAccnt.gettransCount(); i++)
    {
        temp = checkAccnt.getTrans(i);
        message += String.format("%-10d", temp.getTransNumber());
        message += String.format("%-10d", temp.getTransId());
        message += String.format("%10.2f", temp.getTransAmount()) + '\n';
    }
    JOptionPane.showMessageDialog(null, message);
like image 479
Erick Q. Avatar asked Sep 26 '22 04:09

Erick Q.


1 Answers

An alternative to using a fixed width font (as suggested in the comments) or different widgets with better alignment control (as suggested in a different answer) might be to replace the space characters in front of the digits with space characters that have the same width as the digits (if the font has a fixed width for digits, which the screenshot seems to support):

message += String.format("%-10d", temp.getTransNumber()).replace(' ', '\u2007');

See http://www.fileformat.info/info/unicode/char/2007/index.htm

like image 145
Stefan Haustein Avatar answered Oct 11 '22 07:10

Stefan Haustein