2016-03-22 36 views
3

私の行の背景色を設定する必要があります。例えば、これらの行があります(12.01.15上記)と同じ日付の2行がある場合同じ日付の行の背景色を設定する

29.12.14 Absences 
12.01.15 Absences 
12.01.15 Accounts 

は今、彼らは、GUIの同じ背景色を有するべきです。

String week = new String(); 
    int weekCounter = 0; 
    int colour = 0; 

    // Append the rows 
    for (ExcelRow row : printOutRows) { 
     if (weekCounter == 0){ 
     week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(); 
      colour = 0; 
     } 
     else if (row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString().equals(week)){ 
      colour = 0; 
     } 
     else { 
      week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(); 
      colour = 1; 
     } 

     model.addRow(new Object[] { 
       row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(), 
       row.getExcelCells().get(1).getExcelCell().getRichStringCellValue().getString(), 
       row.getExcelCells().get(2).getExcelCell().getRichStringCellValue().getString(), 
       row.getExcelCells().get(3).getExcelCell().getNumericCellValue()}); 

     if (colour == 0){ 
      table.setSelectionBackground(Color.RED); 
     } 
     else{ 
      table.setSelectionBackground(Color.LIGHT_GRAY); 
     } 
     weekCounter ++; 

    } 

上記のコードを試しましたが、JTableのすべての行が白い背景になっています。どのように私の目標に達することができますか?

+2

選択の背景はおそらく行く方法ではありません。問題の行のセルの背景を設定しようとします。 – Thomas

+0

ロジックをTableCellRendererに配置し、レンダラーをテーブルに設定することができます。 setBackground(Color c)はレンダラーでトリックを行う必要があります。 – Endery

+1

[レンダラー/表のエディターの概念](https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) –

答えて

1

最初の列に色変数を追加すると、これを実行できます。私の例では、ブール値を使用しましたが、何か他のものを使用する場合も同様に動作します。

最初の部分では、前と同じようにコードを作成しますが、最初は色を指定します。
その後、ビューから最初の列が削除されるため、表示されません。しかしそれはまだそこにある。
最後に、私が前に設定した変数に出力をレンダリングします。

String week = ""; 
boolean colorSwitch = false; 

for (ExcelRow row : printOutRows) { 
    if (!row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString().equals(week)){ 
     week = row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(); 
     colorSwitch = !colorSwitch; 
    } 

    model.addRow(new Object[] { 
     colorSwitch, 
     row.getExcelCells().get(0).getExcelCell().getRichStringCellValue().getString(), 
     row.getExcelCells().get(1).getExcelCell().getRichStringCellValue().getString(), 
     row.getExcelCells().get(2).getExcelCell().getRichStringCellValue().getString(), 
     row.getExcelCells().get(3).getExcelCell().getNumericCellValue() 
    }); 

} 

// remove first column from the view (the one with the boolean value in it)  
TableColumnModel tcm = table.getColumnModel(); 
tcm.removeColumn(tcm.getColumn(0)); 

// render the table according to the color.  
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){ 
    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { 
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 

     if ((Boolean) table.getModel().getValueAt(row, 0)) { 
      setBackground(Color.BLACK); 
      setForeground(Color.WHITE); 
     } else { 
      setBackground(table.getBackground()); 
      setForeground(table.getForeground()); 
     }  

     return this; 
    } 
    }); 
関連する問題