作業部屋の使い方を試しています。
(empty log message)
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | public class TestTable01 extends javax.swing.JFrame { |
| 19 | 19 | |
| 20 | - TestTableCellRenderer renderer; | |
| 20 | + TestTableCellRenderer tblRenderer; | |
| 21 | 21 | /** |
| 22 | 22 | * Creates new form TestTable01 |
| 23 | 23 | */ |
| @@ -24,8 +24,8 @@ | ||
| 24 | 24 | public TestTable01() { |
| 25 | 25 | initComponents(); |
| 26 | 26 | |
| 27 | - renderer = new TestTableCellRenderer(); | |
| 28 | - jTable1.setDefaultRenderer(Object.class, renderer); | |
| 27 | + tblRenderer = new TestTableCellRenderer(); | |
| 28 | + jTable1.setDefaultRenderer(Object.class, tblRenderer); | |
| 29 | 29 | |
| 30 | 30 | } |
| 31 | 31 |
| @@ -115,9 +115,11 @@ | ||
| 115 | 115 | }// </editor-fold>//GEN-END:initComponents |
| 116 | 116 | |
| 117 | 117 | private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed |
| 118 | - renderer.addRowColumn(1, 1); | |
| 119 | - renderer.addRowColumn(2, 0); | |
| 120 | - renderer.addRowColumn(2, 2); | |
| 118 | + tblRenderer.setBackgroundColor(1, 1, Color.RED); | |
| 119 | + tblRenderer.setBackgroundColor(2, 0, Color.GREEN); // 重複 | |
| 120 | + tblRenderer.setBackgroundColor(2, 0, Color.CYAN); | |
| 121 | + tblRenderer.setBackgroundColor(2, 2, Color.BLUE); | |
| 122 | + tblRenderer.setBackgroundColor(3, 3, Color.YELLOW); | |
| 121 | 123 | jTable1.repaint(); |
| 122 | 124 | }//GEN-LAST:event_jButton1ActionPerformed |
| 123 | 125 |
| @@ -168,9 +170,11 @@ | ||
| 168 | 170 | } |
| 169 | 171 | |
| 170 | 172 | class TestTableCellRenderer extends DefaultTableCellRenderer { |
| 173 | + ArrayList<CellAttribute> arrayCellAttribute = new ArrayList<>(); | |
| 174 | + // ワーク変数 | |
| 175 | + CellAttribute currentCellAttr; | |
| 176 | + Color backgroundColor; | |
| 171 | 177 | |
| 172 | - ArrayList<RowColumn> arrayRowColumn = new ArrayList<>(); | |
| 173 | - | |
| 174 | 178 | TestTableCellRenderer() { |
| 175 | 179 | super(); |
| 176 | 180 | } |
| @@ -181,7 +185,7 @@ | ||
| 181 | 185 | |
| 182 | 186 | Color color = table.getBackground(); |
| 183 | 187 | if(chkRowColumn(row, column)) { |
| 184 | - color = Color.red; | |
| 188 | + color = backgroundColor; | |
| 185 | 189 | } |
| 186 | 190 | c.setBackground(color); |
| 187 | 191 |
| @@ -188,23 +192,32 @@ | ||
| 188 | 192 | return c; |
| 189 | 193 | } |
| 190 | 194 | |
| 191 | - void addRowColumn(int row, int column) { | |
| 192 | - RowColumn inRowColumn = new RowColumn(); | |
| 193 | - inRowColumn.row = row; | |
| 194 | - inRowColumn.column = column; | |
| 195 | - | |
| 196 | - arrayRowColumn.add(inRowColumn); | |
| 195 | + /** | |
| 196 | + * セルの背景色を設定. | |
| 197 | + * @param row 指定するセルの行 | |
| 198 | + * @param column 指定するセルの列 | |
| 199 | + * @param color 指定するセルの背景色 | |
| 200 | + */ | |
| 201 | + public void setBackgroundColor(int row, int column, Color color) { | |
| 202 | + // 重複時削除 | |
| 203 | + if(chkRowColumn(row, column)) { | |
| 204 | + arrayCellAttribute.remove(currentCellAttr); | |
| 205 | + } | |
| 206 | + // 属性設定 | |
| 207 | + CellAttribute cellattr = new CellAttribute(); | |
| 208 | + cellattr.row = row; | |
| 209 | + cellattr.column = column; | |
| 210 | + cellattr.backgroundColor = color; | |
| 211 | + // 登録 | |
| 212 | + arrayCellAttribute.add(cellattr); | |
| 197 | 213 | } |
| 198 | 214 | |
| 199 | - boolean chkRowColumn(int row, int column) { | |
| 215 | + private boolean chkRowColumn(int row, int column) { | |
| 200 | 216 | boolean ans = false; |
| 201 | - | |
| 202 | - RowColumn inRowColumn = new RowColumn(); | |
| 203 | - inRowColumn.row = row; | |
| 204 | - inRowColumn.column = column; | |
| 205 | - | |
| 206 | - for (RowColumn rowcolumn : arrayRowColumn) { | |
| 207 | - if((rowcolumn.row == inRowColumn.row) && (rowcolumn.column == inRowColumn.column)) { | |
| 217 | + for (CellAttribute cellattr : arrayCellAttribute) { | |
| 218 | + if((cellattr.row == row) && (cellattr.column == column)) { | |
| 219 | + currentCellAttr = cellattr; | |
| 220 | + backgroundColor = cellattr.backgroundColor; | |
| 208 | 221 | ans = true; |
| 209 | 222 | break; |
| 210 | 223 | } |
| @@ -211,11 +224,13 @@ | ||
| 211 | 224 | } |
| 212 | 225 | return ans; |
| 213 | 226 | } |
| 214 | - | |
| 215 | - class RowColumn { | |
| 216 | - int row; | |
| 217 | - int column; | |
| 227 | + /** | |
| 228 | + * セル属性. | |
| 229 | + */ | |
| 230 | + class CellAttribute { | |
| 231 | + int row; | |
| 232 | + int column; | |
| 233 | + Color backgroundColor; | |
| 218 | 234 | } |
| 219 | - | |
| 220 | 235 | } |
| 221 | 236 |