Swing/TableBackground のバックアップの現在との差分(No.11)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TableBackground へ行く。
- 1 (2004-08-02 (月) 01:28:45)
- 2 (2004-08-02 (月) 01:34:49)
- 3 (2004-08-04 (水) 02:21:55)
- 4 (2004-10-08 (金) 06:26:43)
- 5 (2004-11-04 (木) 10:12:28)
- 6 (2004-11-19 (金) 09:59:16)
- 7 (2005-01-02 (日) 17:52:49)
- 8 (2005-02-03 (木) 02:04:25)
- 9 (2005-03-25 (金) 02:18:24)
- 10 (2005-04-28 (木) 04:33:09)
- 11 (2005-10-01 (土) 22:26:01)
- 12 (2006-02-27 (月) 16:52:39)
- 13 (2006-11-01 (水) 20:45:30)
- 14 (2007-04-04 (水) 13:08:39)
- 15 (2007-08-21 (火) 13:12:25)
- 16 (2007-08-31 (金) 13:09:40)
- 17 (2008-02-13 (水) 18:36:01)
- 18 (2013-04-11 (木) 21:52:26)
- 19 (2015-10-01 (木) 19:41:47)
- 20 (2016-06-02 (木) 12:27:48)
- 21 (2016-06-20 (月) 14:29:52)
- 22 (2016-06-20 (月) 16:15:38)
- 23 (2016-11-30 (水) 18:14:29)
- 24 (2017-11-26 (日) 23:50:35)
- 25 (2018-10-02 (火) 13:53:03)
- 26 (2020-10-02 (金) 10:12:26)
- 27 (2022-06-11 (土) 15:35:15)
- 追加された行はこの色です。
- 削除された行はこの色です。
#navi(../) *JTableの背景色を変更 [#u3817f74] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2004-08-02~ 更新日:&lastmod; --- category: swing folder: TableBackground title: JTableの背景色を変更 tags: [JTable, JViewport, JScrollPane, JColorChooser] author: aterai pubdate: 2004-08-02T01:28:45+09:00 description: JTableを追加したJViewportがセルで隠れていない部分の色を変更します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTUV7me60I/AAAAAAAAAl4/PQqRFaxI6XA/s800/TableBackground.png --- * 概要 [#summary] `JTable`を追加した`JViewport`がセルで隠れていない部分の色を変更します。 #contents **概要 [#z87d4b66] JTableを追加したJViewportがセルで隠れていない部分の色を変更します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTUV7me60I/AAAAAAAAAl4/PQqRFaxI6XA/s800/TableBackground.png) http://terai.xrea.jp/swing/tablebackground/screenshot.png * サンプルコード [#sourcecode] #code(link){{ // JScrollPane scroll = new JScrollPane(table); scroll.getViewport().add(table); scroll.getViewport().setOpaque(true); scroll.getViewport().setBackground(Color.WHITE); // table.getParent().setBackground(Color.WHITE); }} **サンプルコード [#l5c8f8e1] scroll.getViewport().add(jtable); scroll.getViewport().setOpaque(true); scroll.getViewport().setBackground(Color.white); //jtable.getParent().setBackground(Color.white); * 解説 [#explanation] 上記のサンプルでは、背景色を不透明にするを選択して背景色を設定すると`JViewport`の背景色が変更されます。 -[[サンプルを起動>http://terai.xrea.jp/swing/tablebackground/sample.jnlp]] -[[jarファイル>http://terai.xrea.jp/swing/tablebackground/sample.jar]] -[[ソース>http://terai.xrea.jp/swing/tablebackground/src.zip]] **解説 [#q0eb6822] このページのタイトルなどでJTableの背景色といっている部分は、実際はJTableの背景の色ではなく、JTableを追加したJViewportがセルで隠れていない部分のことです。 - このページのタイトルなどで「`JTable`の背景色」としている部分は実際は`JTable`の背景色ではなく、`JTable`を追加した`JViewport`がセルで隠れていない領域を表している -- このため上記のスクリーンショットのように色をつけたい場合は、`JTable`自身の背景色を設定するのではなく`JViewport`の背景色を設定する必要がある -- サンプルコードでは`JScrollPane`から`JViewport`を取得しているが、`JTable#getParent()`メソッドからも`JViewport`を取得可能 - `JDK 1.6.0`で追加された`JTable#setFillsViewportHeight`メソッドを使用して`JTable`を`JViewport`の高さまで拡張すれば、直接`JTable`の背景色を設定するだけで同様の状態になる -- [[JTable自体の高さを拡張>Swing/FillsViewportHeight]] - セルの背景色を変更する場合は`TableCellRenderer`を使用する -- [[TableCellRendererでセルの背景色を変更>Swing/StripeTable]] - ヘッダの背景色(カラムをマウスでドラッグ中に表示される)を変更する場合は、空の`JViewport`を`JScrollPane`のカラムヘッダに追加してその背景色を変更する #code{{ scroll.setColumnHeader(new JViewport()); //or scroll.setColumnHeaderView(new JLabel()); scroll.getColumnHeader().setBackground(Color.RED); }} このため、上記のスクリーンショットのように色をつけたい場合は、JTable自身の背景色を設定するのではなく、JViewportの背景色を設定する必要があります。サンプルコードではJScrollPaneからJViewportを取得していますが、JTable.getParent()してもJViewportを取得することができます。 * 参考リンク [#reference] - [[JTable自体の高さを拡張>Swing/FillsViewportHeight]] - [[TableCellRendererでセルの背景色を変更>Swing/StripeTable]] - [[JTableに行ヘッダを追加>Swing/TableRowHeader]] 上記のサンプルでは、背景色を不透明にするを選択して背景色を設定してやると、JViewportの背景色が変更されます。 セルの背景色を変更する場合は、TableCellRendererを使用([[TableCellRendererでセルの背景色を変更>Swing/StripeTable]])します。 **参考リンク [#ab7ac14b] -[[TableCellRendererでセルの背景色を変更>Swing/StripeTable]] **コメント [#d3f3a4d2] * コメント [#comment] #comment #comment