简体   繁体   中英

Java - Change colors of some cells in JTable

I have an array of ints called sponsorIndexArr which contains the indices of the cells that I want to change the color of in a table (I also want to make that cell unselectable). The table is one column so I only need the row index of the cell.

Here is some relevant code:

// Configure sponsor table
sponsorstableModel = new DefaultTableModel(sponsorsTableList, new String[]{"Sponsors"}
    @Override
    public boolean isCellEditable(int row, int column) {
       return false;
    }
};
sponsorsTable = new JTable(sponsorstableModel);
sponsorsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sponsorsTable.addMouseListener(this);

sponsorsTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            for (int entry : sponsorIndexArr) {
                System.out.println(entry + " " + row);
                if (entry == row) {
                    System.out.println("HERE");
                    this.setBackground(Color.CYAN);
                    this.setEnabled(false);
                } else {
                    setBackground(null);
                    this.setEnabled(true);
                }
            }
            return this;
        }
    });

The program is printing "HERE" in the correct places. However, what is happening is that only the cell with the last index of sponsorIndexArr is changing colors. When I get rid of setBackground(null) then every cell becomes cyan.

Also when I select any of the other cells the background covers the text. When I get rid of this.setEnabled(true) then I don't have this problem, but then every cell is disabled (text goes gray).

what is happening is that only the cell with the last index of sponsorIndexArr is changing colors.

Your concept of a renderer is wrong. Your renderer has a loop which indicates you are attempting to render all the cells at one time. This is not how a renderer works

The same renderer is used for every cell. Every time a cell needs to be rendered the renderer is invoked. So if you have 10 rows the renderer is called 10 times and the state of the renderer will be updated 10 times to reflect the state of the cell.

I have an array of ints called sponsorIndexArr which contains the indices of the cells that I want to change the color

I would suggest that instead you should use a Set of Integers. Then your renderer will do a simple check to see if the row index is in the set which will then determine how the cell should be rendered.

The code might be something like:

@Override
public Component getTableCellRendererComponent(
    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

    if (isSelected)
        setBackground( table.getSelectionBackground() );
    else if (yourSet.contains(row))
        setBackground( Color.CYAN );
    else
        setBackground( table.getBackground() );

    return this;
}

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