【问题标题】:jTable not coloring row correctlyjTable没有正确着色行
【发布时间】:2018-02-27 07:46:52
【问题描述】:

我有一个问题,基本上我的程序是这样的: 问题是,它有效,它应该将具有“N”的行着色为绿色,但是第一次加载值时,如您所见,第一行有一些白色,但由于某种原因,如果我单击它修复问题的列表,我需要正确着色行而不需要用户单击列表来修复问题,这是我的渲染代码:

public class Render extends JTable {

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, 
            int columnIndex){

        Component componente = super.prepareRenderer(renderer, rowIndex, columnIndex);

        String val = getValueAt(rowIndex, columnIndex).toString();

        if(val.equals("N")){

            componente.setBackground(Color.GREEN);

        }

        return componente;
    }
}

我想我可以使用 Repaint();在 JTable 的 MouseMoved 事件中,但我认为这不是修复它的正确方法......感谢任何帮助,干杯!

【问题讨论】:

  • 您是否尝试在填充表格后调用重绘?
  • 是的先生,同样的问题,只有当我点击列表时它才会正确绘制。
  • !val.equals("N")时该行应该是什么颜色?

标签: java colors jtable row


【解决方案1】:

从改变开始

String val = getValueAt(rowIndex, columnIndex).toString();

String val = getValueAt(rowIndex, 3).toString();

这将确保无论单元格代表什么列,您都在检查适当的列值 - 这就是前导单元格为白色的原因

您还应该考虑在列值不匹配时提供默认颜色

if (val.equals("N")) {

    componente.setBackground(Color.GREEN);

} else {

    componente.setBackground(getBackground());

}

【讨论】:

  • 哦!,解决了,非常感谢!我大约 1 个月前从 delphi 搬到了 java,我还在学习,所以我很感激!晚安!
猜你喜欢
  • 2012-06-23
  • 2012-07-18
  • 1970-01-01
  • 2021-08-10
  • 2021-04-01
  • 1970-01-01
  • 2021-02-20
  • 2018-05-15
  • 1970-01-01
相关资源
最近更新 更多