简体   繁体   中英

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:

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(); in the MouseMoved event in the JTable, but i think is not a proper way to fix it... Any help is appreciated, cheers!

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());

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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