可能重复:
为什么JTable的头不会出现在图像中?
在面板上绘制的JTable的行和列
我的工作中,我要导出的JTable中报告的形式报告的应用程序。 但是,当我尝试直接绘制的JTable上的PDF,它不显示第一行和列的边界。 为此,我尝试用默认的颜色网格左上边框。 这使我在PDF较暗线。 然而,当试图填写任何特定的细胞颜色和它导出的PDF输出,所有的网格线是看不见的。
SimpleTableDemo.java:
package com.swing.table;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.swing.data.BorderCellRenderer;
public class SimpleTableDemo extends JPanel implements ActionListener {
private boolean DEBUG = false;
private int spacing = 6;
private Map columnSizes = new HashMap();
String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years",
"Vegetarian" };
Object[][] data = {
{
"Kathy",
"Smith",
"SnowboardingXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
new Integer(5), new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
final JTable table = new JTable(data, columnNames);
public SimpleTableDemo() {
super(new GridLayout(0, 1));
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
// table.setFillsViewportHeight(true);
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
BorderCellRenderer cellRenderer = new BorderCellRenderer();
cellRenderer.setBorder(new LineBorder(Color.BLACK));
table.setDefaultRenderer(Object.class, cellRenderer);
table.repaint();
// Add the scroll pane to this panel.
add(scrollPane);
JButton button = new JButton("Create Pdf");
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent e)
{
print();
}
private void print() {
Document document = new Document(PageSize.A4.rotate());
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
PdfTemplate pdfTemplate = cb.createTemplate(table.getWidth(), table.getHeight());
Graphics2D g2 = pdfTemplate.createGraphics(table.getWidth(), table.getHeight());
/*g2.setColor(Color.BLACK);
g2.drawRect(x-2, y-2, table.getWidth()+2, table.getHeight()+2);*/
table.print(g2);
cb.addTemplate(pdfTemplate, 20, 20);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
System.err.println(e.getMessage());
}
document.close();
}
/**
Create the GUI and show it. For thread safety, this method should be
invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
BorderCellRenderer.java:
package com.swing.data;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
/**
* @version 1.0 03/06/99
*/
public class BorderCellRenderer extends JLabel
implements TableCellRenderer {
protected Border noFocusBorder;
protected Border columnBorder;
protected Font font;
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public BorderCellRenderer() {
noFocusBorder = new EmptyBorder(1, 2, 1, 2);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setText((value == null) ? "" : value.toString());
Border b;
b = BorderFactory.createCompoundBorder();
// b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,0,1,0,table.getGridColor()));
// b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,0,0,1,table.getGridColor()));
/* if(row == 0 && column == 0)
{
setBackground(Color.CYAN);
}
else
{
setBackground(Color.WHITE);
}
*/
if(row==0 || column == 0)
{
if(row ==0 && column==0)
{
b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,1,0,0,table.getGridColor()));
b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(1,0,0,0,table.getGridColor()));
}
if(row == 0)
{
b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(1,0,0,0,table.getGridColor()));
setBackground(Color.CYAN);
}
if(column == 0)
{
b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,1,0,0,table.getGridColor()));
}
}
else
{
b = table.getBorder();
setBackground(Color.WHITE);
}
setBorder(b);
return this;
}
public void setColumnBorder(Border border) {
columnBorder = border;
}
public Border getColumnBorder() {
return columnBorder;
}
}
请上面的代码试图帮助我获得与网格线和填充单元格颜色一一对应的PDF输出。
更新1: - 我修改的代码actionPerformed()方法,如下: -
public void actionPerformed(ActionEvent e)
{
int x = table.getWidth();
int y = table.getHeight();
BufferedImage bi = null;
if(x>0 && y>0)
{
bi = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
if(bi != null)
{
Graphics g = bi.createGraphics();
table.paint(g);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
}
}
print();
}
谢谢你看看我的代码。 我的要求是不显示表头。 我不想如预期打印头。 这就是为什么我的第一行上边框与第一列左侧缺失。 要纠正这一点,我手动添加一个默认的网格边框到第一列和行。 这让上边框看起来暗灰色。 除了这个我也有填充单元格颜色。 它看起来很好,当我打印到缓冲图像,但后来在PDF它会从下面的截图我的网格线: -
JTable中的BufferedImage输出
JTable中PDF输出