TITLE:JTableのセルにJCheckBoxを表示して行背景色を変更

Posted by terai at 2008-07-14

JTableのセルにJCheckBoxを表示して行背景色を変更

JTableのセルに表示されているJCheckBoxがチェックされていれば、その行の背景色を変更します。

  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

JTable table = new JTable(model) {
  @Override
  public Component prepareEditor(TableCellEditor editor, int row, int column) {
    Component cmp = super.prepareEditor(editor, row, column);
    if(convertColumnIndexToModel(column) == BOOLEAN_COLUMN) {
      //System.out.println("JTable: prepareEditor");
      JCheckBox c = (JCheckBox)cmp;
      c.setBackground(c.isSelected()? Color.ORANGE:getBackground());
    }
    return cmp;
  }
  @Override
  public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component c = super.prepareRenderer(renderer, row, column);
    Boolean isChecked = (Boolean) model.getValueAt(
      convertRowIndexToModel(row), BOOLEAN_COLUMN);
    c.setForeground(getForeground());
    c.setBackground(isChecked? Color.ORANGE:getBackground());
    return c;
  }
};

解説

上記のサンプルでは、JTableのprepareEditor、prepareRendererをオーバーライドしてセルエディタや行の背景色を変更しています。


以下のように、TableCellRendererやCellEditorを利用する方法もあります。

private static JTable makeTable2(final DefaultTableModel model) {
  final JTable table = new JTable(model);
  TableColumnModel columns = table.getColumnModel();
  for(int i=0;i<columns.getColumnCount();i++) {
    columns.getColumn(i).setCellRenderer(new RowColorTableRenderer(
      table.getDefaultRenderer(model.getColumnClass(i))));
  }
  DefaultCellEditor dce = (DefaultCellEditor)table.getDefaultEditor(
        model.getColumnClass(BOOLEAN_COLUMN));
  final JCheckBox check = (JCheckBox)dce.getComponent();
  check.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      System.out.println("JCheckBox: focusGained");
      JCheckBox c = (JCheckBox)e.getSource();
      c.setBackground(c.isSelected()?Color.ORANGE:table.getBackground());
    }
  });
  table.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
      int col = table.columnAtPoint(e.getPoint());
      if(table.convertColumnIndexToModel(col)==BOOLEAN_COLUMN) {
        System.out.println("JTable: mousePressed");
        int row = table.rowAtPoint(e.getPoint());
        Boolean isChecked = (Boolean) model.getValueAt(
            table.convertRowIndexToModel(row), BOOLEAN_COLUMN);
        check.setBackground(isChecked?Color.ORANGE:table.getBackground());
      }
    }
  });
  return table;
}
static class RowColorTableRenderer implements TableCellRenderer {
  private final Color evenColor = new Color(240, 240, 255);
  private final TableCellRenderer renderer;
  public RowColorTableRenderer(TableCellRenderer renderer) {
    this.renderer = renderer;
  }
  public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    Component c = renderer.getTableCellRendererComponent(table, value,
        isSelected, hasFocus, row, column);
    TableModel model = table.getModel();
    Boolean isChecked = (Boolean) model.getValueAt(
        table.convertRowIndexToModel(row), BOOLEAN_COLUMN);
    c.setForeground(table.getForeground());
    c.setBackground(isChecked?Color.ORANGE:table.getBackground());
    return c;
  }
}

参考リンク

コメント

  • ごちそうさまでした。 -- tanuchan