简体   繁体   English

jTable无法正确着色行

[英]jTable not coloring row correctly

i have a problem, basically my program looks like this: 我有一个问题,基本上我的程序如下所示: 例 the thing is, it works, it is supposed to color the rows that have "N" in green, but the first time it loads the values, as you can see, the first row have bits of white, but for some reason if i click the list it fixes the issue, i need the rows to be colored properly without the user needing to click on the list to fix the issue, this is my Render code: 事实是,它可以正常工作,应该用绿色将具有“ 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;
    }
}

I figured i could use Repaint(); 我想我可以使用Repaint(); in the MouseMoved event in the JTable, but i think is not a proper way to fix it... Any help is appreciated, cheers! 在JTable的MouseMoved事件中,但是我认为这不是修复它的正确方法...感谢您的帮助,干杯!

Start by changing 从改变开始

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

to

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

This will ensure that no matter what column the cell represents, you are checking the appropriate columns value - this is why the leading cells are white 这样可以确保无论该单元格代表哪一列,您都在检查适当的列值-这就是为什么前导单元格为白色的原因

You should also consider providing a default color for when the column values doesn't match 您还应该考虑为列值不匹配时提供默认颜色

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

    componente.setBackground(Color.GREEN);

} else {

    componente.setBackground(getBackground());

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM