我是靠谱客的博主 自觉糖豆,最近开发中收集的这篇文章主要介绍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.

最后

以上就是自觉糖豆为你收集整理的java自定义表格渲染器,如何正确使用自定义渲染器绘制JTable中的特定单元格?...的全部内容,希望文章能够帮你解决java自定义表格渲染器,如何正确使用自定义渲染器绘制JTable中的特定单元格?...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部