java - How can I clip strings in Java2D and add ... in the end? -


I am trying to print invoices in Java swing applications. By enforcing printable and implementing the method Public Int Print (Graphics G, Page Format PF, Int page) .

I want to draw the wire in the columns, and when the string is long, I want to clip it and end it with "...". How can I measure the string and how can I clip it to the correct position?

Some of my code:

  font title = new font ("Times New Roman", font bold, 14); G2d.setFont (title); Font matrix matrix = g2d Tag fontmatrix (title); G2d.drawString (myString, 0, 20);  

How can I restrict the maximum of 120px to myString ?

I metrics.stringWidth (myString) , but I could not find a situation where I have to clip the string.

Expected results can be:

  A long string which exc ... A short string is another long string, but oak  
< / Div>

You can get a good estimate with the string Width divided by the number of characters to get the average width of each character Accordingly, after that you can see the clip distance that how many characters you can fit in. Approximate distance from the beginning (for the nickname two or three) ... and finally move your above location.

Verify new string is not a clip - if this happens, make some adjustments as needed. Eventually, if you have WWWWWWWWiiiiii, you probably will need to adjust it. But in all, this approach will be very fast.


Comments