如何给jtable单元格提供不同的背景颜色

时间:2015-01-22 05:56:16

标签: java swing colors hashmap tablecellrenderer

我只获得了最后一行的颜色..

class CustomTableCellRenderer extends DefaultTableCellRenderer {

    private Map<Integer, Color> mapColors;

    public CustomTableCellRenderer() {
        mapColors = new HashMap<>();
    }

    public void setRowColor(int row, Color color) {
//        System.out.println(row + "...happy...." + color);
        mapColors.put(row, color);

    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
        Color color = mapColors.get(row);
        if (color != null) {
            cell.setBackground(color);
        } else {
            cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        }
        row++;
        return cell;
    }
}

1 个答案:

答案 0 :(得分:5)

单元格渲染器是一个共享资源,它应用于给定列的所有单元格。为了给定一行或多行提供自定义,您需要提供一种方法来记住您想要的行和它的颜色,例如......

public class CustomTableCellRenderer extends DefaultTableCellRenderer {

    private Map<Integer, Color> mapColors;

    public CustomTableCellRenderer() {
        mapColors = new HashMap<Integer, Color>();
    }

    public void setRowColor(int row, Color color) {
        mapColors.put(row, color);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

        Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
        Color color = mapColors.get(row);
        if (color != null) {
            cell.setBackground(color);
        } else {
            cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
        }
        return cell;
    }
}

例如......

Rows

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.HashMap;
import java.util.Map;
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 Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultTableModel model = new DefaultTableModel(new Object[]{"One"}, 6);
                CustomTableCellRenderer renderer = new CustomTableCellRenderer();
                renderer.setRowColor(0, Color.RED);
                renderer.setRowColor(1, Color.GREEN);
                renderer.setRowColor(2, Color.BLUE);
                renderer.setRowColor(3, Color.PINK);
                renderer.setRowColor(4, Color.MAGENTA);
                JTable table = new JTable(model);
                table.getColumnModel().getColumn(0).setCellRenderer(renderer);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    class CustomTableCellRenderer extends DefaultTableCellRenderer {

        private Map<Integer, Color> mapColors;

        public CustomTableCellRenderer() {
            mapColors = new HashMap<Integer, Color>();
        }

        public void setRowColor(int row, Color color) {
            mapColors.put(row, color);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {

            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, 1);
            Color color = mapColors.get(row);
            if (color != null) {
                cell.setBackground(color);
            } else {
                cell.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            }
            return cell;
        }
    }
}

有关详细信息,请参阅Concepts: Editors and Renderers