java自定义表格渲染器,如何正确使用自定义渲染器绘制JTable中的特定单元格?...

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:

Updo5.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值