|
import java.awt.*;
import javax.swing.*;
import com.wildcrest.j2printerworks.*;
class J2TablePrinterSimplestTest{
static public void main(String args[]){
Object[] columns = {"This", "is", "a", "JTable"};
Object[][] data = {{"table", "", "", ""}, {"", "data", "", ""},
{"", "", "goes", ""}, {"", "", "", "here"}};
JTable table = new JTable(data, columns);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(400,83));
JFrame frame = new JFrame("J2TablePrinter test");
frame.getContentPane().add(scrollPane);
frame.pack();
frame.setVisible(true);
J2Printer printer = new J2Printer();
printer.setSeparatePrintThread(false);
J2TablePrinter tablePrinter = new J2TablePrinter(table);
printer.addPageable(tablePrinter);
printer.print();
System.exit(0);
}
}
Most of the methods of J2TablePrinter are set and get methods for controlling its property values. The full list of J2TablePrinter methods, what they do, and their default values are given in the J2TablePrinter Javadoc documentation.
General properties
Whether J2TablePrinter is used as a Pageable or a Flowable, you can control the print area (rows and columns) to be printed, gridlines and outside lines, whether it is left, right, or center justified horizontally on a page, top, bottom, or center justified within the remaining space on a page, whether the column heading are replicated on every page, only on the top pages, or not at all
Print area
J2TablePrinter allows you to specify a subarea of contiguous JTable rows and columns to be printed. The subarea of the JTable may be specified using the J2TablePrinter method:
setPrintArea (int startColumnIndex, int startRowIndex, int numberOfPrintColumns, int numberOfPrintRows)
Set the print area to be printed.
If either firstColumnIndex or firstRowIndex is less than 0 or past the end of the table, the value 0 will be used instead. If either numberOfPrintingColumns or numberOfPrintingRows is less than or equal to 0, then the printing area will extend through the rightmost column or bottommost row, respectively. Likewise, if the values of firstColumnIndex+numberOfPrintingColumns or firstRowIndex+numberOfPrintingRows extend beyond the rightmost column or bottommost row, the printing area will end at the rightmost column or bottommost row, respectively.
The method getPrintArea() returns in a Rectangle object the values of firstColumnIndex, firstRowIndex, numberOfPrintingColumns, and numberOfPrintingRows as the Rectangle's x, y, width, and height values, respectively. The values returned will be the actual values for the current JTable that result from the most recent setPrintArea call.
The print area feature can easily be adapted to implementing a "print selection" feature common to many client applications since you can query the current selection and set the print area accordingly. In addition, non-default page breaks can be implemented by using the print area feature to print successive page-sized areas of your own choosing.
Border and gridline control
Since J2TablePrinter performs its imaging using the JTable print and paint methods, you can control the printing of your gridlines using the JTable methods setGridColor(), setShowHorizontalLines(), and setShowVerticalLines().
JTable does not itself include a border, and J2TablePrinter does not print any Border you may have surrounding your JTable. Instead, as a convenience, J2TablePrinter will optionally print a hairline border around the outside of your JTable with the same appearance (color, etc.) as your JTable gridlines. This feature may be turned on or off using the J2TablePrinter method:
setOutsideLines (boolean showOutsideLines)
Indicate whether to add outside lines when printing JTable
Beginning with J2TablePrinter 1.3, the hairline will also be drawn around the JTable column headings if the column heading printing feature is turned on and if setShowOutsideLines is set to true.
« »
|