如何将我的JTable中第一行的颜色设置为黄色(或任何颜色)? [英] How to set the colour of my 1st row in my JTable to yellow(or any colour)?

查看:264
本文介绍了如何将我的JTable中第一行的颜色设置为黄色(或任何颜色)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将一列设置为黄色,但我无法将行设置为黄色。

I am able to set one column to yellow but I am unable to set a row to yellow.

以下代码用于列:

TableColumn col = mytable.getColumnModel().getColumn(0);

col.setCellRenderer(new MyTableCellRenderer());

我该怎么做一行呢?

我在网上尝试了教程和示例,但它总是将整个表格描绘为黄色而不是仅仅一行。

I have tried the tutorials and examples on the net but it always paints the whole table yellow rather than just one row.

谢谢

推荐答案

您需要做的是生成自定义TableCellRenderer。有关详细信息,请参阅本教程。您的渲染器需要测试传入的行索引并确定它是否为0行。

what you need to do is generate a custom TableCellRenderer. see this tutorial for detail. your renderer will need to test the row index that is passed in and determine whether it is row 0 or not.

 public Component getTableCellRendererComponent(JTable table,
                                    Object value,
                                    boolean isSelected,
                                    boolean hasFocus,
                                    int row,
                                    int column) {
     if (row == 0) {
         setBackground(myBGColor)
     }
     ....

这篇关于如何将我的JTable中第一行的颜色设置为黄色(或任何颜色)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆