Swing/FillsViewportHeight のバックアップ差分(No.7)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/FillsViewportHeight へ行く。
- 1 (2007-08-20 (月) 11:28:51)
- 2 (2007-08-20 (月) 15:35:51)
- 3 (2007-08-21 (火) 13:21:59)
- 4 (2007-08-22 (水) 10:59:22)
- 5 (2007-08-24 (金) 15:41:28)
- 6 (2008-04-04 (金) 19:18:47)
- 7 (2008-08-26 (火) 16:00:37)
- 8 (2011-03-13 (日) 03:32:09)
- 9 (2013-02-01 (金) 11:31:56)
- 10 (2015-01-07 (水) 16:20:24)
- 11 (2016-04-19 (火) 16:01:55)
- 12 (2016-05-31 (火) 14:55:35)
- 13 (2017-03-28 (火) 15:11:28)
- 14 (2018-02-06 (火) 15:26:32)
- 15 (2018-10-19 (金) 16:52:36)
- 16 (2020-10-15 (木) 10:00:29)
- 17 (2022-08-03 (水) 11:01:16)
- 追加された行はこの色です。
- 削除された行はこの色です。
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.setFillsViewportHeight(true); }} #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); c.setBackground((row%2==0)?evenColor:getBackground()); } 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)を適用するかどうかを切り替えることができます。 - (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) #screenshot(,screenshot1.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