Search code examples
javajframejtablejpanelconways-game-of-life

Java - Jtable - cells with different colors


I already saw some examples, but still couldn't understand how to solve this issue, there is not a clean solution probably.. I am trying to build John Conway's Game Of Life by using Jtable with 10*10 as cells. I know how to change the table background color(all cells'), but i can't do it for a specific cell color change. I know, i need to create a class with CellRenderer, but I couldn't manage to understand it too..

Here is my code :

public class theGame {

    public static void main(String [] args) {

        theMatrix gui = new theMatrix();

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(550,450);
        gui.setVisible(true);
        gui.setTitle("Game Of Life");



        JOptionPane.showMessageDialog(null, "  Welcome John Conway's Game Of Life  ");
        int replay = JOptionPane.showConfirmDialog(null, "Would you like to see the next generation?", "Close?", JOptionPane.YES_NO_OPTION);

        if (replay == JOptionPane.YES_OPTION);

    }
}

public class theMatrix extends JFrame { 

    JTable table;

    public theMatrix() {
        setLayout(new FlowLayout());
        String[] columNames = {"", "", "", "", "", "", "", "", "", ""};

        Object[][] data = { // Create a 10*10 table with default values.

            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null},
            {null, null, null, null, null, null, null, null, null, null}
        }

        table = new JTable(data, columNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 500));
        table.setRowHeight(40);
        table.setBackground(Color.lightGray);

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);

    }
}

Solution

  • Here is an example for a custom cell rendered, which changes background color based on row and column:

    public class TheMatrix extends JFrame {
    
        JTable table;
    
        public TheMatrix() {
            setLayout(new FlowLayout());
            String[] columNames = {"", "", "", "", "", "", "", "", "", ""};
    
            Object[][] data = { // Create a 10*10 table with default values.
    
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            };
    
            table = new JTable(data, columNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 500));
            table.setRowHeight(40);
            table.setDefaultRenderer(Object.class, new MyRenderer());
    
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
        }
    }
    
    class MyRenderer implements TableCellRenderer {
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                        boolean hasFocus, int row, int col) {
            JTextField cell = new JTextField();
    
            cell.setBackground(((row % 2) == 0) && ((col % 2) == 0) ? Color.WHITE : Color.BLACK);
            return cell;
          }
    }