TITLE:JTable自体の高さを拡張
#navi(../)
*JTable自体の高さを拡張 [#va78a74c]
Posted by [[terai]] at 2007-08-20

#contents

**概要 [#c553adfb]
JDK 6 で導入された機能を使用して、JViewportの高さまでJTableを拡張します。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#j4e564fb]
#code{{
table = new JTable(model) {
  public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
    Component c = super.prepareRenderer(tcr, row, column);
    if(isRowSelected(row)) {
      c.setForeground(getSelectionForeground());
      c.setBackground(getSelectionBackground());
    }else{
      c.setForeground(getForeground());
      c.setBackground((row%2==0)?evenColor:oddColor);
    }
    return c;
  }
  public JPopupMenu getComponentPopupMenu() {
    return makePop();
  }
};

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBackground(Color.RED);
scrollPane.getViewport().setBackground(Color.GREEN);
table.setBackground(tablebgColor);
//table.setBackground(scrollPane.getBackground());

table.setComponentPopupMenu(new JPopupMenu());
//scrollPane.getViewport().setComponentPopupMenu(makePop());
//scrollPane.setComponentPopupMenu(makePop());

table.setRowSorter(new TableRowSorter<TableModel>(model));

Box box = Box.createHorizontalBox();
box.add(new JCheckBox(new AbstractAction("FillsViewportHeight") {
  public void actionPerformed(ActionEvent evt) {
    JCheckBox cb = (JCheckBox)evt.getSource();
    table.setFillsViewportHeight(cb.isSelected());
  }
}));
box.add(new JButton(new AbstractAction("clearSelection") {
  public void actionPerformed(ActionEvent evt) {
    table.clearSelection();
  }
}));
}}

**解説 [#y553338e]
上記のサンプルでは、チェックボックスの選択状態で、JTable#setFillsViewportHeight(boolean)を適用するかどうかを切り替えることができます。

- (getFillsViewportHeight() == false) の場合(デフォルト値)
-- 下部の余白はJTableではないため、JViewportの背景色(緑)が表示される
--- [[JTableの背景色を変更>Swing/TableBackground]]
-- JScrollPane、またはJViewportにsetComponentPopupMenuしたり、リスナーを設定していないため、下部の余白で右クリックしてもポップアップメニューは無効

- (getFillsViewportHeight() == true) の場合
-- JTableの高さがJViewportの高さより小さい時は、両者が同じ高さになるようにJTableが拡張される
-- JTable#setBackgorund(Color)で設定した色(薄い黄色)がJTable下部の余白の背景色となる
-- JTable自体が拡張されるため、余白部分を右クリックしてもポップアップメニューが表示される
--- 縦スクロールバーとテーブルヘッダで出来る余白(赤)などはJScrollPaneなので、ポップアップメニューは無効
-- 簡単に余白部分にドロップしたり、空のJTableにドロップすることができる
--- [[JTableの行をドラッグ&ドロップ>Swing/DnDTable]]では、余白にドロップ出来ない
--- [[Fileのドラッグ&ドロップ>Swing/FileListFlavor]]では、DropTargetをJTable、JViewPortの両方に設定する必要がある

ちなみに、JScrollPane、JViewportの背景色も以下のように表示されることがあるので、実際に使う場合はtable.setBackground(scrollPane.getBackground())するなどして、すべておなじ色になるようにしておいた方がいいかもしれません。
#screenshot(,screenshot2.png)

- scrollPane.setBackground(Color.RED);
-- 縦スクロールバーとテーブルヘッダで出来る余白の色
- scrollPane.getViewport().setBackground(Color.GREEN);
-- 列をドラッグして移動する場合の隙間の色
-- JTable#setAutoResizeMode(JTable.AUTO_RESIZE_OFF)としたときの右余白

**参考リンク [#oc8c4477]
-[[JTableの背景色を変更>Swing/TableBackground]]
-[[TableCellRendererでセルの背景色を変更>Swing/StripeTable]]
-[[Fileのドラッグ&ドロップ>Swing/FileListFlavor]]
-[[JTable becomes uglier with AUTO_RESIZE_OFF - Santhosh Kumar's Weblog>http://www.jroller.com/santhosh/entry/jtable_becomes_uglier_with_auto]]

**コメント [#r893ee1b]
#comment