更改我的JTable中特定行的颜色

更改我的JTable中特定行的颜色

本文介绍了更改我的JTable中特定行的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何更改JTable中某些行的颜色的方法,这些行在名为Notfoundrow的整数矢量中具有索引,但是导致表中所有行的颜色都变为红色的问题!!

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 have as result all the rows in the Table change color to Red !!

这是我的代码:

package essai_trafficclass;
import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MonCellRenderer extends DefaultTableCellRenderer {

    public static ArrayList<Integer> Notfoundrow1 = OneWayRelation.Notfoundrow;

    public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell = super.getTableCellRendererComponent(table, value,
                        isSelected, hasFocus, row, column);
        int L = 0;
        while (L < Notfoundrow1.size()) {
            System.out.println("la valeur du vecteur " + Notfoundrow1.get(L));
            if (row == Notfoundrow1.get(L) && column == 1) {
                cell.setBackground(Color.RED);
            } else if (row == Notfoundrow1.get(L) && column == 1) {
                cell.setBackground(Color.RED);
            } else {
                cell.setBackground(Color.WHITE);
            }
            L++;
        }
        return cell;
    }
}

然后我通过以下方式致电此类:

And then I Call this class by :

tableM.setDefaultRenderer(Object.class, new MonCellRenderer());

tableM是我要更改其行颜色的表.

tableM is the table that i want change the color if its rows.

谢谢您的帮助.

推荐答案

您可以大大简化您的逻辑...

You could simplify your logic considerably...

而不是您的while循环,请利用API的可用功能...

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);
}
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-您可能还想通读代码约定Java编程语言

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

这篇关于更改我的JTable中特定行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:30