Problem: when the user prints the JTextArea, the text at the right-hand side of the "paper" is cut off, despite the fact that it is visible on the screen. Question: How can this be solved? I've tried two ways of printing the text area, and neither has worked as I'd hoped. Unfortunately, I do not have a printer for my laptop so I can't do tests easily. Does anyone know of a better way to do the printing?
public class MatchListing extends JPanel implements Printable, ActionListener{
private JFrame frame;
private JButton print;
private JTextArea textArea;
public void actionPerformed(ActionEvent e){
//alternatively to the below:
try{
textArea.print();
} catch(Exception excp){}
}
public MatchListing(){
// ...
textArea = new JTextArea(10, 25);
textArea.setMargin(new Insets(0,100,0,0));
textArea.setFont(new Font("Times New Roman", Font.PLAIN, 18));
textArea.setEditable(false);
JScrollPane jsp = new JScrollPane(textArea);
// put text into the textArea here & add it to the frame
//(code removed for readability)
add(jsp, c);
add(print);
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException{
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now print the window and its visible contents */
frame.printAll(g);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
}