I am trying to print invoices in Java Swing applications. I do this by expanding Printableand implementing the method public int print(Graphics g, PageFormat pf, int page).
I would like to draw rows in columns, and when the row is long, I want her clip and let it end with "...". How to measure a line and copy it to the desired position?
Some of my code:
Font headline = new Font("Times New Roman", Font.BOLD, 14);
g2d.setFont(headline);
FontMetrics metrics = g2d.getFontMetrics(headline);
g2d.drawString(myString, 0, 20);
Ie How can I limit the myStringmaximum to 120px?
I could use metrics.stringWidth(myString), but I do not get the position where I need to pin the string.
Expected results may be:
A longer string that exc...
A shorter string.
Another long string, but OK
Jonas source
share