简体   繁体   English

用Java打印JTable和其他文本字段

[英]Printing JTable and other textfields in Java

I am developing a desktop app in Java for my project work. 我正在为我的项目工作开发一个Java桌面应用程序。 I need to print an invoice with sub total and total fields along with tabular data from JTable . 我需要打印一张包含子总计和总字段的发票以及来自JTable表格数据。 I am able to print the table but not in the same page. 我能够打印表格,但不能在同一页面打印。 ie JTable prints in first page and subtotal, total prints in next page. 即JTable打印在第一页和小计,总打印在下一页。 Is there a way to print both data in the same page. 有没有办法在同一页面中打印两个数据。 I dont want to use reporting engines. 我不想使用报告引擎。 Just use the inbuilt java printing services. 只需使用内置的java打印服务。 Please guide me. 请指导我。

Print Format as I want: 我想要的打印格式:

在此输入图像描述

Following figure shows the GUI 下图显示了GUI

在此输入图像描述

And following figure shows the report i am getting till now. 下图显示了我到目前为止的报告。 Its not in correct format. 它的格式不正确。 Please help 请帮忙

在此输入图像描述

Code is as follows 代码如下

        JLabel title = new JLabel();
        title.setText("Invoice");
        title.setBounds(300, 200, 80, 30);


        JTextField subTotal = new JTextField();
        subTotal.setText("Sub Total : Rs. " + SubTotal.getText());
        subTotal.setBounds(400, 200, 150, 30);


        MyPrintable prt = new MyPrintable();
        prt.addComponent(title);
        prt.addComponent(billTable); //billTable is the JTable
        prt.addComponent(subTotal);
        prt.printIt();

And below is the My Printable class 以下是我的Printable课程

class MyPrintable implements Printable
{
private ArrayList<JComponent> items;

public MyPrintable()
{
    items = new ArrayList<>();
}
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
    if(page > 0) return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(pf.getImageableX(), pf.getImageableY());


    for(JComponent item : items)
    {
        item.setLayout(null);
      //  item.setBounds(500, 500, 200, 200);
        item.printAll(g);
        g2.translate(0, item.getHeight());
    }
    return Printable.PAGE_EXISTS;
}

public void printIt()
{
    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();;
    PrinterJob job = PrinterJob.getPrinterJob();
    try
    {
        job.setPrintable(this);
        if(job.printDialog(attributes))
            job.print(attributes);
    }
    catch (PrinterException e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
}

public void addComponent(JComponent component) { items.add(component); }
}

If you want to print all your stuff at the coordinates you've set, you either need to create a new panel, lay out all your component to print in this panel, and than print this panel or you need to translate all the components in your print method to the correct coordinates. 如果要在所设置的坐标处打印所有内容,则需要创建一个新面板,在此面板中布置要打印的所有组件,然后打印此面板或者需要翻译所有组件您的打印方法到正确的坐标。

the second way will look like 第二种方式看起来像

public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
    if(page > 0) return Printable.NO_SUCH_PAGE;
    Graphics2D g2 = (Graphics2D)g;
    g2.translate(pf.getImageableX(), pf.getImageableY());


    for(JComponent item : items)
    {
        g2.translate(item.getX(), item.getY());
        item.printAll(g);
        g2.translate(-item.getX(), -item.getY()); // bring all back to default coordinates
    }
    return Printable.PAGE_EXISTS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM