0

に表示されているデータベース全体がありJTable、印刷ボタンを追加しましたが、それは、のデータをファイルに変換し、JTableExcel.csvコマンドでそれを開くことであり、ユーザーはそれを印刷できますが、かなり見苦しく見えます。JTableコンポーネントをプリンターに送信する方法はありますか?

4

2 に答える 2

0

JTableと呼ばれるメソッドがありprint()ます。あなたをたくさん救うでしょう。下記参照 :-

package com.tanyasis.librarymanager;

import java.awt.HeadlessException;
import java.awt.print.PrinterException;
import java.text.MessageFormat;

import javax.swing.JTable;

/**
 * Used to provide printing information and adding information that might be
 * important in a page such as page header, contents and footer. This class
 * makes sure that all contents of a table fit in the given page.
 * 
 * @author Tanyasis Mwanik
 * 
 */
public class PrintTable {

    private JTable table;
    private MessageFormat headerFormat, footerFormat;

    /**
     * Prints the table and provide post printing information to the user if it
     * was succesful
     * 
     * @param table
     *            <code>JTable</code> to be printed
     * @param tableTitle
     *            <code>String</code> to be used as the table header/title
     */
    public PrintTable(JTable table, String tableTitle) {
        // TODO Auto-generated constructor stub
        this.setTable(table);
        // Sets the table header
        headerFormat = new MessageFormat(tableTitle);
        // Sets the table footer
        footerFormat = new MessageFormat("Page {0}");

        try {
            boolean complete = table.print(JTable.PrintMode.FIT_WIDTH,
                    headerFormat, footerFormat, true, null, true, null);

            if (complete) {
                new ConfirmationClass(
                        "<html><h2>Records Printed Successfully</h2></html>");
            }

        } catch (HeadlessException | PrinterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * @return the table
     */
    public JTable getTable() {
        return table;
    }

    /**
     * @param table
     *            the table to set
     */
    public void setTable(JTable table) {
        this.table = table;
    }
}
于 2012-10-16T13:46:18.623 に答える