Ok it may simple. But can't figure it out.
I have a JPanel that contains a JTable. JTable contains few rows, Sometime more, because the table model i push into it depends on database.
However, i don't use any JScollpane that enclose my JTable. As a result, when JTable contains more and more row, parent JPanel automatically resized its height. That is working fine.
Problem is i want to print whole JPanel at a time. may be it needs several page, i don't care. I can print JTable directly with a header and footer. But in my case JPanel contains some important component like JLable. So, there is no other way to avoid printing of JPanel.
I search several online link, everywhere i found a suggestion to implements printable interface.
so i implements printable in my class and overload print....
@Override
public int print(Graphics arg0, PageFormat arg1, int arg2) throws PrinterException {
Graphics2D g2d = (Graphics2D) arg0;
g2d.translate((int) arg1.getImageableX(), (int) arg1.getImageableY());
float pageWidth = (float) arg1.getImageableWidth();
float pageHeight = (float) arg1.getImageableHeight();
float imageHeight = (float) paintPanel.getHeight();
float imageWidth = (float) paintPanel.getWidth();
float scaleFactor = Math.min((float) pageWidth / (float) imageWidth, (float) pageHeight / (float) imageHeight);
int scaledWidth = (int) (((float) imageWidth) * scaleFactor);
int scaledHeight = (int) (((float) imageHeight) * scaleFactor);
BufferedImage canvas = new BufferedImage(paintPanel.getWidth(), paintPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D gg = canvas.createGraphics();
paintPanel.paint(gg);
Image img = canvas;
g2d.drawImage(img, 0, 0, scaledWidth, scaledHeight, null);
return Printable.PAGE_EXISTS;
}
Its not working.
Another problem that is my second problem. I want to place a footer JPanel in every page. So how could it possible.
Help me please. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…