Change the color of specific rows in my JTable

前端 未结 1 497
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 06:35

I am looking for how to change the color of some rows in my JTable which have index in an integer vector called Notfoundrow, but the problem that I hav

相关标签:
1条回答
  • 2021-01-23 06:57

    You could simplify your logic considerably...

    Rather then your while loop, take advantage of the available functionality of the API...

    if (column == 1 || Notfoundrow1.contains(row)) {
        setBackground(Color.RED);
    } else {
        setBackground(Color.WHITE);
    }
    

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.EventQueue;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    
    public class TestCellRenderer02 {
    
        public static void main(String[] args) {
            new TestCellRenderer02();
        }
        private List<Integer> notFound = new ArrayList<>(25);
    
        public TestCellRenderer02() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    Random rand = new Random(System.currentTimeMillis());
                    DefaultTableModel model = new DefaultTableModel(new Object[]{"A", "B"}, 0);
                    for (int index = 0; index < 100; index++) {
                        model.addRow(new Object[]{index, index});
                        if (rand.nextBoolean()) {
                            notFound.add(index);
                            System.out.println("Not found @ " + index);
                        }
                    }
    
                    JTable table = new JTable(model);
                    table.setDefaultRenderer(Object.class, new MonCellRenderer());
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(table));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class MonCellRenderer extends DefaultTableCellRenderer {
    
            public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value,
                                isSelected, hasFocus, row, column);
    
                if (column == 1 || notFound.contains(row)) {
                    setBackground(Color.RED);
                } else {
                    setBackground(Color.WHITE);
                }
                return this;
            }
        }
    }
    

    ps- You might also like to take a read through Code Conventions for the Java Programming Language

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