概述
I have a JTable component in my GUI which displays psuedocode of an algorithm. I want to highlight the current line of execution by changing the background of a particular cell and then changing the cell beneath and so on.
Right now my code changes the backgrounds on all cells in my JTable as pictured below:
The code I am using to archive this current state is as below:
class CustomRenderer extends DefaultTableCellRenderer
{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JLabel d = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if((row == 0) && (column == 0))
d.setBackground(new java.awt.Color(255, 72, 72));
return d;
}
}
I then call jTable2.setDefaultRenderer(String.class, new CustomRenderer()); in my constructor.
I assume that:
This method is being called on every String type table cell.
That this would only change the colour of the cell at position (0,0)
How do I fix my code so that only cell (0,0) is coloured?
解决方案
Add an else clause to your if:
if ((row == 0) && (column == 0)) {
d.setBackground(new java.awt.Color(255, 72, 72));
}
else {
d.setBackground(Color.WHITE);
}
Remember that the same renderer instance is used to paint all the cells.
最后
以上就是自觉糖豆为你收集整理的java自定义表格渲染器,如何正确使用自定义渲染器绘制JTable中的特定单元格?...的全部内容,希望文章能够帮你解决java自定义表格渲染器,如何正确使用自定义渲染器绘制JTable中的特定单元格?...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复