Search code examples
htmljlabel

Monospaced html formatting jlabel


I am creating an output window for a program dealing with matrices. It is supposed to print out the preformed command along with a formatted version of the matrix. But I am having problems with the alignment. I know the String.format works because i have a toString() method that works correctly.

https://i.sstatic.net/rqfzG.png

Notice how the second and third rows are not correctly spaced. This is because the 100.00 has completely filled the formatted string where as the 0.00's need extra spaces to fill the string(see toHtml()). I believe this has something to do with the way that the HTML is being displayed but im not sure. My guess is that the spaces behind the zeros are not being displayed properly or are being combined.

Here are the methods involved.

public String toHtml(int dec)
{
    String[] colors = {"#C0C0C0","#FFFFFF"};
    String f = "%-"+(getLongestValue()+dec+1)+"."+dec+"f";
    String res = "";
    for(int r = 0;r<rows;r++)
    {
        for(int c = 0;c<columns;c++)
        {
            res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+String.format(f, contents[r][c])+"</span>";
        }
        res += "<p>";
    }
    return res;
}

which creates the HTML text to be displayed. The method getLongestValue() returns the largest length of any number before its decimal place in the array 'contents'.

and

newOutput("New Matrix ["+name+"]<br>"+m.toHtml());

public void newOutput(String s)
{
    JLabel l = new JLabel("<html>"+s+"<br></html>");
    l.setFont(new Font("Monospaced",1,18));
    jPanel1.add(l);
}

which adds the label to the output window

Also, here is the toString() method for reference

public String toString()
{
    String f = "%-"+(getLongestValue()+3)+".2f ";
    String res = "";
    for(int r = 0;r<rows;r++)
    {
        for(int c = 0;c<columns;c++)
        {
            res += String.format(f, contents[r][c]);
        }
        res += "\n";
    }
    return res;
}

output of the Matrix through toString() toString Output

A more extreme version https://i.sstatic.net/ZQKnB.png

In this case the program should have found that the largest values were -15 or -20 and set the size of the format length to 6( 3 for the length, 2 for the decimal places and 1 for the decimal) but instead it doesnt appear that any of the values, besides the two I mentioned, are following the format.

Here is the output of toString() for the previous example toString() output


Solution

  • This fixes it, the spaces arent being represented correctly as monospaced

    public String toHtml(int dec)
    {
        String[] colors = {"#C0C0C0","#FFFFFF"};
        String f = "%-"+(getLongestValue()+dec+2)+"."+dec+"f";
        String res = "";
        for(int r = 0;r<rows;r++)
        {
            for(int c = 0;c<columns;c++)
            {
                res += "<span style=\"background-color:"+colors[(r+c)%2]+";\">"+
                        String.format(f, contents[r][c]).replaceAll("\\s", ((char)160)+"")+"</span>";
            }
            res += "<p>";
        }
        return res+"";
    }