0

How to change the state of each cell using a 2D boolean array

I have attached a Jtable code. I like to remove the "oneCount, twoCount..." because overtime there will get unmaintainable as the grid size increases. I would like to keep a 2D Array of boolean values to indicate whether a cell should be highlighted or not. A mouse pressed on the cell would toggle the value. The renderer would check the value do the the highlighting. In my renderer I want to keep a 2D Array to represent the state of each cell. I have a MouseListener and I the row and column of the cell. I want to toggle the state of the cell in the renderer and invoke repaint() on the table. I initialized the 2D boolean array and it should be false by default. When I assign it to true I get the exception "java.lang.ArrayIndexOutOfBounds Exception" boolean cells[][] = new boolean[row][column]; cells[row][column] = true; How should I add a 2D boolean array to change the state of each cell? https://code.sololearn.com/cQ1cVEjne87q/?ref=app

1st Feb 2023, 11:28 AM
Sibusiso Mbambo
7 Answers
+ 1
Noting you had ArrayIndexOutOfBounds exception and the snippet in post Description, I think you need to recall that arrays' valid bounds are from 0 up to (not including) the dimension. So for that 2D array named <cells> which has <row> rows and <column> columns; valid row indices are from 0 ~ <row> - 1, and valid column indices are from 0 ~ <column> - 1. Now look at it again ... cells[ row ][ column ] = true; That is the line that raised the exception cause you try to access an element with invalid index.
1st Feb 2023, 1:03 PM
Ipang
+ 1
In which part (line numbers) did you write the portion involving the use of <cells>? I don't immediately see it in the attached code. You can assign/modify the array's elements as long as the indices used are valid. How the row & column index are generated? by random? or sequentially? this at least, will decide how you approach the goal.
1st Feb 2023, 1:15 PM
Ipang
0
Hi Mirielle, thanks for your reply. My idea of the code is to highlight each cell individually and be able to un-highlight each cell individually.
1st Feb 2023, 11:59 AM
Sibusiso Mbambo
0
So how should I call it. Should i use a loop with cells.length
1st Feb 2023, 1:07 PM
Sibusiso Mbambo
0
I've edited the code and used the boolean variable isSelected to print whether the cells boolean 2D array changes from false to true but does not change so I can use the boolean on the change color method. ---------- class TableRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { boolean cells[][] = new boolean [4][4]; Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if(isSelected) { if(cells[0][0] == false) { System.out.println("cell 0 0 is " + cells[0][0]); cells[0][0] = true; } else if(cells[0][0] == true) { System.out.println("cell 0 0 is " + cells[0][0]); } } return c; } }
3rd Feb 2023, 1:40 PM
Sibusiso Mbambo