“Highlighting” specific rows in a JTable

前端 未结 2 1793
广开言路
广开言路 2020-12-18 17:06

I would like to highlight specific rows in a JTable whenever the contents of the a cell match with an input from the user. The following code is what I have that works thus

2条回答
  •  眼角桃花
    2020-12-18 17:50

    The renderers are "rubber stamps". That basically means that they carry there previous settings over to the next cell.

    What you need to do is provide a "default" behavior

    if (!isRowSelected(row) ) {
        c.setBackground((hashMapcontainer
            .containsKey(row)) ? Color.GREEN
            : getBackground());
    } else {
    
        // Define the default background color
        // Don't forget to take into the selection state
    
    }
    

    While I personally think that prepareRenderer in this case is probably a fair solution, you really should explore the possibly of providing a base line renderer. This is a lot of work to get right but has the advantage of been portable (if you change table implementations) as well as allowing other people the chance to define the highlight rules of a given cell, which you've basically just gone and overridden, IMHO.

    I'd also suggest taking a look at JXTable as it has in built highlighting

提交回复
热议问题