1

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

    }
}
Itsko
  • 29
  • 5
  • 1
    [Concepts: Editors and Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) and [Using Custom Renderers](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer) – MadProgrammer Apr 15 '17 at 23:11
  • 1
    A cell renderer should use the data for a specific cell to make determinations about how it should be rendered – MadProgrammer Apr 15 '17 at 23:16
  • Yea sure, I will change my table for 0,1 and then will try to colored them in a different types as the game should be.. – Itsko Apr 15 '17 at 23:20
  • Color change how? What color for what value? And always show your best attempt code with your question to create a better question and get better help. – Hovercraft Full Of Eels Apr 16 '17 at 00:04
  • Thanks! I think I understand how to solve it with help of this answer -- [link](http://stackoverflow.com/questions/14563799/jtable-cellrenderer-changes-backgroundcolor-of-cells-while-running/14565614#14565614) – Itsko Apr 16 '17 at 00:47

1 Answers1

1

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;
      }
}
c0der
  • 18,467
  • 6
  • 33
  • 65