我们需要在pdf上绘制JTable。对于这个要求,我采用了JTable并直接打印到pdf上。但是我没有获得JTable的Left和Top边框。另外,我需要为表格切割单元格边框。 无论如何,我可以为JTable中的单元格赋予不同的颜色边框吗? 例如: -
Left Border = Grid Color
Top Border = Grid Color
Right Border = Black Color
Bottom Border = Grid Color
任何与此相关的建议都会非常有用吗?
答案 0 :(得分:5)
在打印前向表格添加MatteBorder。
Color color = UIManager.getColor("Table.gridColor");
MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
table.setBorder(border);
答案 1 :(得分:5)
@andrewthompson提供的链接应该为您提供表格边框打印问题的第一部分答案(a.k.a。Why does the JTable header not appear in the image?)
要在表格中获得不同颜色的内部边框(我相信这是您的第二个问题),您必须结合使用MatteBorder
和CompoundBorder
以及{{1} }}
TableCellRenderer
答案 2 :(得分:0)
感谢您的所有代码。我真的很感谢通过贡献他们的时间和精力来帮助我们的人。那么所有这些解决方案都将解决问题。我用以下代码得到了很好的解决方案: -
package com.swing.data;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableExample{
public static void main(String[] args) {
final JFrame f = new JFrame("TableExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(createTable());
f.pack();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
static JComponent createTable() {
final JTable table = new JTable(9,9) {
private static final long serialVersionUID = 0;
Color B = Color.RED;
Color C = this.getGridColor();
final Border[][] borders = {
{new ZoneBorder(C,B,B,B), new ZoneBorder(B,C,C,C), new ZoneBorder(B,B,C,C)},
{new ZoneBorder(C,C,C,B), new ZoneBorder(C,C,C,C), new ZoneBorder(C,B,C,C)},
{new ZoneBorder(C,C,B,B), new ZoneBorder(C,C,B,C), new ZoneBorder(C,B,B,C)}
};
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component result = super.prepareRenderer(renderer, row, column);
if (result instanceof JComponent) {
if(row == 0 && column == 0)
((JComponent) result).setBorder(borders[0][0]);
}
return result;
}
};
table.setRowHeight(28);
//table.setGridColor(Color.BLACK);
TableColumnModel tcm = table.getColumnModel();
for(int c = 0; c<table.getColumnCount(); ++c) {
TableColumn tc = tcm.getColumn(c);
tc.setPreferredWidth(28);
}
JPanel inner = new JPanel(new GridLayout());
//inner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
inner.add(table);
return inner;
}
}
package com.swing.data;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ZoneBorder implements Border {
private static final int WIDTH = 1;
private Color colorN, colorE, colorS, colorW;
public ZoneBorder(Color colorN, Color colorE, Color colorS, Color colorW) {
this.colorN=colorN;
this.colorE=colorE;
this.colorS=colorS;
this.colorW=colorW;
}
public boolean isBorderOpaque() {
return false;
}
public Insets getBorderInsets(Component c) {
return new Insets(WIDTH,WIDTH,WIDTH,WIDTH);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Color old = g.getColor();
if (colorN != null) {
g.setColor(colorN);
g.fillRect(x, y, width, WIDTH);
}
if (colorE != null) {
g.setColor(colorE);
g.fillRect(x+width-WIDTH, y, WIDTH, height);
}
if (colorS != null) {
g.setColor(colorS);
g.fillRect(x, y+height-WIDTH, width, WIDTH);
}
if (colorW != null) {
g.setColor(colorW);
g.fillRect(x, y, WIDTH, height);
}
g.setColor(old);
}
}
谢谢!