how to give a jtable cell different background colour

眉间皱痕 提交于 2019-12-24 12:37:59

问题


i am getting only last row coloured..

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:


A cell renderer is a shared resource which is applied to ALL the cells for a given column. In order to provide customisation for a given row or rows, you need to provide a means by which you can remember which row you want and it's color, for example...

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;
    }
}

For example...

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;
        }
    }
}

See Concepts: Editors and Renderers for more details



来源:https://stackoverflow.com/questions/28082098/how-to-give-a-jtable-cell-different-background-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!