Java JTable 头渲染器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19069050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTable header renderer
提问by Alosyius
I'm trying to make the background of my JTable
header have a gradient.
我试图让我的JTable
标题的背景有一个渐变。
This works fine using the code:
使用以下代码可以正常工作:
String[] colName = new String[] { "#", "Location", "Name" ,"Source", "System", "Hits", "Current", "Time" };
Object[][] products = new Object[][] {
};
dtm = new DefaultTableModel(products, colName) {
@Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
table = new JTable( dtm );
table.setRowHeight(32);
table.getTableHeader().setReorderingAllowed(false);
table.setDefaultRenderer(Object.class, new VisitorRenderer());
table.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 12));
table.getColumnModel().getColumn(0).setMinWidth(5);
table.getColumnModel().getColumn(1).setMinWidth(50);
table.getColumnModel().getColumn(2).setMinWidth(5);
table.getColumnModel().getColumn(3).setMinWidth(5);
table.getColumnModel().getColumn(4).setMinWidth(5);
table.getColumnModel().getColumn(5).setMinWidth(3);
table.getColumnModel().getColumn(6).setMinWidth(230);
table.getColumnModel().getColumn(7).setMinWidth(3);
final JTableHeader header = table.getTableHeader();
header.setPreferredSize(new Dimension(100, 37));
header.setDefaultRenderer(new DefaultTableCellHeaderRenderer() {
private Object selectedColumn;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0,
new java.awt.Color(237, 237, 237), 0, getHeight(),
new java.awt.Color(222, 222, 222));
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
@Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
DefaultTableCellHeaderRenderer rendererComponent = (DefaultTableCellHeaderRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (selectedColumn == value) {
rendererComponent.setBorder(BorderFactory.createCompoundBorder(rendererComponent.getBorder(), BorderFactory.createLineBorder(Color.red, 1)));
rendererComponent.setHorizontalAlignment(SwingConstants.LEFT);
} else {
rendererComponent.setBorder(BorderFactory.createCompoundBorder(rendererComponent.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
rendererComponent.setHorizontalAlignment(SwingConstants.CENTER);
}
if (column == 0) {
rendererComponent.setForeground(header.getForeground());
} else {
rendererComponent.setForeground(header.getForeground());
}
return rendererComponent;
}
});
However for some reason all I cannot see my border and text, it seems like the text also got the gradient applied?
但是由于某种原因,我看不到我的边框和文本,似乎文本也应用了渐变?
I'd like to have the text black and a colored border between my columns. Any ideas?
我希望在我的列之间使用黑色文本和彩色边框。有任何想法吗?
回答by trashgod
The class, sun.swing.table.DefaultTableCellHeaderRenderer
, is not a good choice for this. Instead, obtain a reference and wrap it in your implementation of TableCellRenderer
, as shown here. See also Darryl Burke's Default Table Header Cell Renderer.
类 ,sun.swing.table.DefaultTableCellHeaderRenderer
不是一个好的选择。取而代之的是,要获得一个基准,敷在你的实现TableCellRenderer
,如图所示这里。另请参阅 Darryl Burke 的默认表头单元格渲染器。