如何从 Java 应用程序将 JTable 数据发送到打印作业?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6136629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:30:43  来源:igfitidea点击:

How to Send JTable data to Print Job from Java Application?

javaswingprintingjtable

提问by Saher Ahwal

In my Application, I have a JTable with data and I need to add a print button which adds the functionality of printing the data in a table on printer paper (send a printer job), how is that possible using the PrinterJob'class? I've been searching around for examples, but I couldn't find a simple example that prints some data from a JComponent.

在我的应用程序中,我有一个带有数据的 JTable,我需要添加一个打印按钮,该按钮添加了在打印纸上的表格中打印数据的功能(发送打印机作业),这怎么可能使用PrinterJob'该类?我一直在四处寻找示例,但找不到从 JComponent 打印一些数据的简单示例。

This is one of the Websites I found: http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html. But I am not sure what to focus on to understand how printing works (page format ...etc).

这是我找到的网站之一:http: //java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html。但我不确定要关注什么来了解打印的工作原理(页面格式......等)。

Thanks

谢谢

回答by Boro

I solved the problem by painting the whole table to a printer page, resizing it accordingly etc. As printing is in fact painting just to a different receiver.

我通过将整个表格绘制到打印机页面,相应地调整其大小等来解决这个问题。因为打印实际上只是绘制到不同的接收器。

@Saher please check my answer to other questionwhere I present links which were useful for me in understanding how the API works/can be applied.

@Saher 请检查我对其他问题的回答,其中我提供的链接对我理解 API 的工作方式/可以应用的方式很有用。

EDIT:

编辑:

Please do check the tutorials especially no. 2. The way I am doing it is that I have a MyPrintUtilityclass which implements Printable interface. Its constructor takes a component I want to print. When it takes the component it calculates how many pages it will take and stores page end points. Then I have a method I call for it that initailizes the print dialog.

请检查教程,尤其是没有。2. 我这样做的方式是我有一个实现 Printable 接口的 MyPrintUtilityclass。它的构造函数接受一个我想打印的组件。当它使用组件时,它会计算将需要多少页并存储页面端点。然后我有一个我调用它的方法来初始化打印对话框。

    public void print()
    {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        if(printJobName != null)
            printJob.setJobName(printJobName);
        printJob.setPrintable(this);    
        if(printJob.printDialog())
            try
            {
                //for faster printing, turn off double buffering
                disableDoubleBuffering(componentToPrint);
                System.out.println("Calling PrintJob.print()");
                printJob.print(new HashPrintRequestAttributeSet());
                System.out.println("End PrintJob.print()");
            }catch(PrinterException pe)
            {
                System.out.println("Error printing: " + pe);
            }finally//whatever happend (exception or not) turn back on the double buffering
            {
                enableDoubleBuffering(componentToPrint);
            }
    }

In the public int print(Graphics g, PageFormat pf, int pageIndex)method of the Printable interface I translate the graphic accordingly clipping the table for each page.

public int print(Graphics g, PageFormat pf, int pageIndex)Printable 界面的方法中,我相应地翻译了图形,为每一页剪切了表格。

More resources to check:

要检查的更多资源:

http://www.java2s.com/Code/Java/2D-Graphics-GUI/PrintinJavaMultipage.htm--- printing on many pages

http://www.java2s.com/Code/Java/2D-Graphics-GUI/PrintinJavaMultipage.htm--- 在多页上打印

http://download.oracle.com/javase/tutorial/2d/printing/gui.html--- printing GUI components.

http://download.oracle.com/javase/tutorial/2d/printing/gui.html--- 打印 GUI 组件。

http://www.sideofsoftware.com/print_preview_tutorial.htm--- print preview tutorial

http://www.sideofsoftware.com/print_preview_tutorial.htm--- 打印预览教程

http://www.java-tips.org/java-se-tips/java.awt.print/print-the-text-file-and-print-preview-them.html--- a fantastic example of how to preview and print text files.

http://www.java-tips.org/java-se-tips/java.awt.print/print-the-text-file-and-print-preview-them.html--- 一个很好的例子预览和打印文本文件。

EDIT2:

编辑2:

You must seriously read this one.Here author explains how splitting on many pages works and how to translate the graphics and move the clip etc.

你必须认真阅读这一篇。在这里,作者解释了如何在多页上拆分以及如何翻译图形和移动剪辑等。

回答by mKorbel

hmmm,

嗯,

todays Java6 Oracle tutotial about printing http://download.oracle.com/javase/tutorial/2d/printing/index.htmlcontains http://download.oracle.com/javase/7/docs/api/javax/swing/JTable.html#print%28%29which is described in JTable Tutorialincluding Runnable Examples, then you have to search (on this forum too) for correct Print paginations and orientations

今天关于打印的 Java6 Oracle tutotial http://download.oracle.com/javase/tutorial/2d/printing/index.html包含 http://download.oracle.com/javase/7/docs/api/javax/swing/ JTable.html#print%28%29JTable 教程中进行了描述,包括Runnable 示例,然后您必须(也在此论坛上)搜索正确的打印分页和方向

for example

例如

PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
set.add(OrientationRequested.LANDSCAPE);
resultFxTable.print(JTable.PrintMode.FIT_WIDTH, header, footer, false, set, false);

note: I'm not sure that is possible correctly set Font, FontSize and FOntColor for PrintHeader and PrintFooter for JTable Printing

注意:我不确定是否可以为 JTable 打印正确设置 PrintHeader 和 PrintFooter 的 Font、FontSize 和 FOntColor