删除 jtable 中的单元格边框
我有自定义单元格渲染器,并且想要删除单元格的边框。
我该怎么做?我尝试了 setBorder 但它不起作用。
这是我的渲染器代码:
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;
}
}
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.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单元格之间绘制的线不是单元格本身的一部分。它们是由表格绘制的。您可以使用以下方法为整个表格关闭它们:
仅禁用水平线或垂直线:
或者,您可以使用以下方法更改线条的颜色:
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:
To disable just the the horizontal or just the vertical lines:
Or, you can change the color of the lines with:
我不知道你的代码是如何编译的,因为只有 Swing 组件可以有 Border,而 Component 类没有 setBorder() 方法。
当我覆盖默认渲染器时,我使用如下内容:
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.
When I override the default renderer I use something like: