Change background color of JTable row based on column value

后端 未结 1 1723
时光说笑
时光说笑 2020-12-11 08:32

hi i am new in java jtable cellrendered. I am looking for a way that works in my program but i dont have any luck finding it. Here is my Jtable

Employee ID          


        
相关标签:
1条回答
  • 2020-12-11 09:23

    "i want to change the background/foreground of the rows which has a value of "inactive" in status column"

    It's really just a matter of getting the value from table/model. if the status for that row is inactive, then set the background/foreground for every every cell in that row. Since the renderer is rendered for every cell, then basically you need to get the value of the [row][statusColumn], and that will be the status value for each row. Something like

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
    
            String status = (String)table.getModel().getValueAt(row, STATUS_COL);
            if ("active".equals(status)) {
                setBackground(Color.BLACK);
                setForeground(Color.WHITE);
            } else {
                setBackground(table.getBackground());
                setForeground(table.getForeground());
            }       
            return this;
        }   
    });
    

    Here's a simple example

    enter image description here

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    public class TableRowDemo {
    
        private static final int STATUS_COL = 1;
    
        private static JTable getTable() {
            final String[] cols = {"col 1", "status", "col 3"};
            final String[][] data = {
                    {"data", "active", "data"},
                    {"data", "inactive", "data"},
                    {"data", "inactive", "data"},
                    {"data", "active", "data"}
            };
            DefaultTableModel model = new DefaultTableModel(data, cols);
            return new JTable(model) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return new Dimension(350, 150);
                }
            };
        }
    
        private static JTable getNewRenderedTable(final JTable table) {
            table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
                @Override
                public Component getTableCellRendererComponent(JTable table,
                        Object value, boolean isSelected, boolean hasFocus, int row, int col) {
                    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
                    String status = (String)table.getModel().getValueAt(row, STATUS_COL);
                    if ("active".equals(status)) {
                        setBackground(Color.BLACK);
                        setForeground(Color.WHITE);
                    } else {
                        setBackground(table.getBackground());
                        setForeground(table.getForeground());
                    }       
                    return this;
                }   
            });
            return table;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    JOptionPane.showMessageDialog(null, new JScrollPane(getNewRenderedTable(getTable())));
                }
            });
        }
    }
    

    Another option is to @Override prepareRenderer of the table. It will give you the same result.

    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    
    public class TableRowDemo {
    
        private static final int STATUS_COL = 1;
    
        private static JTable getTable() {
            final String[] cols = {"col 1", "status", "col 3"};
            final String[][] data = {
                    {"data", "active", "data"},
                    {"data", "inactive", "data"},
                    {"data", "inactive", "data"},
                    {"data", "active", "data"}
            };
            DefaultTableModel model = new DefaultTableModel(data, cols);
            return new JTable(model) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return new Dimension(350, 150);
                }
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
                    Component c = super.prepareRenderer(renderer, row, col);
                    String status = (String)getValueAt(row, STATUS_COL);
                    if ("active".equals(status)) {
                        c.setBackground(Color.BLACK);
                        c.setForeground(Color.WHITE);
                    } else {
                        c.setBackground(super.getBackground());
                        c.setForeground(super.getForeground());
                    }
                    return c;
                }
            };
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    JOptionPane.showMessageDialog(null, new JScrollPane(getTable()));
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题