• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTable自体の高さを拡張
#navi(../)
*JTable自体の高さを拡張 [#va78a74c]
Posted by [[terai]] at 2007-08-20
---
category: swing
folder: FillsViewportHeight
title: JTable自体の高さを拡張
tags: [JTable, JScrollPane, JViewport, JPopupMenu]
author: aterai
pubdate: 2007-08-20T11:28:51+09:00
description: JDK 6で導入された機能を使用して、JViewportの高さまでJTableを拡張します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMkfiP8jI/AAAAAAAAAZY/qHWqJtrcUgQ/s800/FillsViewportHeight.png
---
* 概要 [#summary]
`JDK 6`で導入された機能を使用して、`JViewport`の高さまで`JTable`を拡張します。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTMkfiP8jI/AAAAAAAAAZY/qHWqJtrcUgQ/s800/FillsViewportHeight.png)

**概要 [#c553adfb]
JDK 6 で導入された機能を使用して、JViewportの高さまでJTableを拡張します。
* サンプルコード [#sourcecode]
#code(link){{
JTable table = new JTable(model);
table.setFillsViewportHeight(true);

-&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();
  }
}));
JScrollPane scroll = new JScrollPane(table);
scroll.setBackground(Color.RED);
scroll.getViewport().setBackground(Color.GREEN);
// table.setBackground(Color.BLUE);
// table.setBackground(scroll.getBackground());
}}

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

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

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

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

- scrollPane.setBackground(Color.RED);
-- 縦スクロールバーとテーブルヘッダで出来る余白の色
- scrollPane.getViewport().setBackground(Color.GREEN);
#ref(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTMm5lGwGI/AAAAAAAAAZc/VWaIAURiCKk/s800/FillsViewportHeight1.png)

- `scrollPane.setBackground(Color.RED)`
-- 縦スクロールバーとテーブルヘッダで生成される余白の色
- `scrollPane.getViewport().setBackground(Color.GREEN)`
-- 列をドラッグして移動する場合の隙間の色
-- JTable#setAutoResizeMode(JTable.AUTO_RESIZE_OFF)としたときの右余白
-- `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]]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTable.html#setFillsViewportHeight-boolean- JTable#setFillsViewportHeight(boolean) (Java Platform SE 8)]
- [[JTableの背景色を変更>Swing/TableBackground]]
- [[TableCellRendererでセルの背景色を変更>Swing/StripeTable]]
- [[Fileのドラッグ&ドロップ>Swing/FileListFlavor]]
- [http://www.jroller.com/santhosh/entry/jtable_becomes_uglier_with_auto JTable becomes uglier with AUTO_RESIZE_OFF - Santhosh Kumar's Weblog]

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