0
我有一個在JTable
中顯示的整個數據庫,我添加了一個打印按鈕,但是它的作用是將JTable
中的數據轉換爲.csv
文件,excel打開它並用戶可以打印它,但它看起來像很難看。有沒有辦法將JTable
組件發送給打印機?如何打印JTable?
我有一個在JTable
中顯示的整個數據庫,我添加了一個打印按鈕,但是它的作用是將JTable
中的數據轉換爲.csv
文件,excel打開它並用戶可以打印它,但它看起來像很難看。有沒有辦法將JTable
組件發送給打印機?如何打印JTable?
根據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;
}
}
你試過:[JTable.print()](http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#print( ))? – aymeric
聽起來像一個智能按鈕:-)嚴重的是,作爲編碼器,你應該知道按鈕的作用(畢竟這是你的代碼)。如果它應該做其他事情,..代碼不同。順便說一句:側邊欄提到了可能對學習有用的類似問題。 – kleopatra