I have a jPanel with a bunch of jLabel's and other components. When i print the jPanel the text in the jLabel is cutoff. How can i fix this?
Here is my printing code
//handle the printing
private class Prints implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = dashHolderPanel.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = pageFormat.getImageableHeight();
double pWidth = pageFormat.getImageableWidth();
double pXStart = pageFormat.getImageableX();
double pYStart = pageFormat.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
// print it
dashHolderPanel.print(g2);
return Printable.PAGE_EXISTS;
}
}
Here is the code for the print Button
private void printDashButtonMouseClicked(java.awt.event.MouseEvent evt) {
try {
//print the dash
if (evt.getClickCount() == 1) {
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setJobName("Print ROP Dash");
//set the attributes of the page formatter
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(Fidelity.FIDELITY_TRUE);
attr_set.add(PrintQuality.HIGH);
attr_set.add(PrintQuality.HIGH);
attr_set.add(OrientationRequested.LANDSCAPE);
attr_set.add(MediaSizeName.NA_LETTER);
float width = (float) MediaSize.NA.LETTER.getX(MediaSize.INCH);
float height = (float) MediaSize.NA.LETTER.getY(MediaSize.INCH);
attr_set.add(new MediaPrintableArea(0.25f, 0.25f, width - 0.5f, height - 0.5f, MediaPrintableArea.INCH));
//get the formatter
PageFormat pf2 = pjob.pageDialog(attr_set);
//If user does not hit cancel then print.
if (pf2 != null && pjob.printDialog() == true) {
//Set print component
pjob.setPrintable(new Prints(), pf2);
//print the dash
pjob.print();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
That is the job of a layout manager. You can try this:
label.setSize( label.getPreferredSize() );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.