How to change the color of a JTable entire row having a particular column value

前端 未结 3 1269
时光取名叫无心
时光取名叫无心 2020-12-21 11:18

I have a Jtable which gets populated from an array of values. My code is like this:

  private static final String[] columnNames = {\"Line Number\", \"Error\"         


        
相关标签:
3条回答
  • 2020-12-21 11:45

    One of the problems with the render API is that it's difficult to provide compound renderers. There are ways to do, don't get me wrong, but it would have been nice to have it built in...[end rant]...

    The basic idea is you you want to set up a series of renderers that extend from a base renderer which contains the logic required to determine what it should do under the required conditions.

    public class FilterRenderer extends 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); 
            Double percent = (Double) table.getValueAt(row, 3);
            // You'll need some way to supply the filter value, may via a centralised 
            // manager of some kind.
            if (percent > 0.3 && !isSelected) {
                setOpaque(true);
                setBackground(Color.RED);
            } else {
                setOpaque(false);
            }
            return this;
        }
    }
    
    public class OtherCellRenderer extends FilterRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
            // Apply any special renderer requirements, like translating an object value to String
            return this;
        }
    }
    

    You'll need a custom renderer for each column (from you example, that's 4) and apply each to the table column

    TableColumnModel model = table.getColumnModel();
    model.getColumn(0).setCellRenderer(new LineNumberRenderer());
    model.getColumn(1).setCellRenderer(new ErrorRenederer());
    model.getColumn(2).setCellRenderer(new FixProposedRenderer());
    model.getColumn(3).setCellRenderer(new Percentage());
    

    Or you could just use SwingLabs JXTable which has inbuilt support for row highlighters

    0 讨论(0)
  • 2020-12-21 11:59

    You can create a custom cell renderer. In its implementation, check if percentage value is > 30 for a given row, then highlight this cell.

    For example:

    class SomeRenderer extends DefaultTableCellRenderer {
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
    
            Component c = super.getTableCellRendererComponent(table,
                    value, isSelected, hasFocus, row, column);
    
            if (isHighlightingEnabled){
                Integer percentage = (Integer) table.getValueAt(row, 3);
                if (percentage > 30)
                    c.setBackground(Color.RED);
            }
            return c;
        }
    }
    

    You may enable/disable this rendering logic upon action if needed.

    See Using Custom Renderers for more details.

    0 讨论(0)
  • 2020-12-21 12:03

    See the approach in Table Row Rendering for a solution without creating custom renderers.

    You may also want to check out Table Format Renderers so you can format the percentage column easily.

    0 讨论(0)
提交回复
热议问题