选择行时突出显示单元格 [英] Highlight cell when row is selected

查看:130
本文介绍了选择行时突出显示单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是,当我选择一行时,具有自定义单元格渲染器的单元格不会突出显示该单元格,但是会突出显示其他单元格.

The problem I am having is that when I select a row, the cell with a custom cell renderer doesn't highlight that cell but does the other cells.

public Component getTableCellRendererComponent(
    JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column)
{
    setFont(ApplicationStyles.TABLE_FONT);

    if (value != null)
    {
        BigDecimal decimalValue = toBigDecimal(value);
        decimalValue =
            decimalValue.setScale(2, BigDecimal.ROUND_HALF_EVEN);

        DecimalFormat formatter = new DecimalFormat("$##,##0.00");
        formatter.setMinimumFractionDigits(2);
        formatter.setMinimumFractionDigits(2);
        String formattedValue = formatter.format(value);
        setText(formattedValue);
    }
    return this;
}

推荐答案

格式化数据最简单的方法是覆盖默认渲染器的setValue(...)方法,如

The easiest way to format data is to override the setValue(...) method of the default renderer as demonstrated in the Swing tutorial on Using Custom Renderers. Then you don't have to worry about the highlighting.

或更简单的方法是使用表格格式渲染器,那么您只需提供渲染器要使用的Format.

Or maybe easier is to use the Table Format Renderer then all you need to provide is the Format to be used by the renderer.

这篇关于选择行时突出显示单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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