java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2655字,纯文字阅读大概需要4分钟。
内容图文
![java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个](/upload/InfoBanner/zyjiaocheng/680/4fc27daa9794440f886a3da52a69acc9.jpg)
我有一个JTable,希望我可以更改单击的单个单元格的颜色.
这是我的代码的简化版本:
public class TableFrame extends JFrame {
public TableFrame() {
JTable table = new JTable(8, 8);
table.setGridColor(Color.BLACK);
table.setDefaultRenderer(CustomCellRenderer.class, new CustomCellRenderer());
getContentPane().add(table);
}
public class CustomCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (hasFocus) {
l.setBackground(Color.red);
l.setText("Hello");
}
return l;
}
}
}
当我单击某个单元格时,我希望它可以将颜色更改为红色并向其中添加“ Hello”.它会更改文本,但是出于某些奇怪的原因,它会更改之后的所有单元格的颜色吗?当我单击一个未着色的单元格时,它会执行相同的操作,但是如果有意义的话,并不一定总是以一种有组织的方式进行?就像,它不会为之后的所有单元格上色,但也许是一些刚好在上面的单元格,而其他则保持空白.
这真的很奇怪,毫无意义.怎么了??
解决方法:
在仔细研究DefaultTableCellRenderer类之后,当您在JLabel组件上调用setBackground时,该组件将支持DefaultTableCellRenderer,它将存储您使用的值.
public void setBackground(Color c) {
super.setBackground(c);
unselectedBackground = c;
}
再次绘制单元格时,将使用此值(unselectedBackground)在“默认”模式下重新绘制单元格…
if (isSelected) {
//...
} else {
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}
super.setForeground(unselectedForeground != null
? unselectedForeground
: table.getForeground());
super.setBackground(background);
}
这意味着,当您使用setBackground并将其传递给Color.RED时,DefaultTableCellRenderer会假定它成为所有未选中单元格的默认颜色.
您唯一的选择是手动重置背景颜色,例如…
public class CustomCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (hasFocus) {
l.setBackground(Color.red);
l.setText("Hello");
} else if (!isSelected) {
l.setBackground(table.getBackground());
}
return l;
}
}
另外,您实际上应该使用更多类似…
table.setDefaultRenderer(Object.class, new CustomCellRenderer());
注册单元格渲染器,因为它是TableModel#getColumnClass返回的Class类型,它确定使用哪个单元格渲染器;)
内容总结
以上是互联网集市为您收集整理的java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个全部内容,希望文章能够帮你解决java-TableCellRenderer将颜色设置为许多单元,而不仅仅是一个所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。