Java 删除jtable中的单元格边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3167112/
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
remove cells border in a jtable
提问by harshit
I have my on custom cell renderer and want to remove the border of the cell.
How can i do it? I tried setBorder but it doesn't work.
我有我的自定义单元格渲染器,想删除单元格的边框。
我该怎么做?我试过 setBorder 但它不起作用。
Here is my renderer code:
这是我的渲染器代码:
public class MyTableCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = -1195682136616306875L;
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (!isSelected) {
if (row % 2 == 0 && row != 1) {
c.setBackground(new Color(255, 255, 150));
} else {
c.setBackground(Color.WHITE);
}
} else {
c.setBackground(new Color(255, 230, 255));
}
c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
return c;
}
}
采纳答案by Devon_C_Miller
The lines drawn between cells are not part of the cells themselves. They are drawn by the table. You can turn them off for the entire table with:
单元格之间绘制的线不是单元格本身的一部分。它们是由桌子绘制的。您可以通过以下方式为整个表关闭它们:
table.setShowGrid(false);
To disable just the the horizontal or just the vertical lines:
要仅禁用水平线或仅禁用垂直线:
table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);
Or, you can change the color of the lines with:
或者,您可以使用以下命令更改线条的颜色:
table.setGridColor(color)
回答by camickr
I don't know how your code compiles since only Swing components can have a Border and the Component class doesn't have a setBorder() method.
我不知道你的代码是如何编译的,因为只有 Swing 组件可以有一个 Border,而 Component 类没有 setBorder() 方法。
When I override the default renderer I use something like:
当我覆盖默认渲染器时,我使用类似的东西:
Class CustomRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
this.setBorder (BorderFactory.createBevelBorder (EtchedBorder.RAISED));
return this;
}
}