Swing/AutoWrapTableCell のバックアップ差分(No.12)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/AutoWrapTableCell へ行く。
- 1 (2010-10-25 (月) 14:24:03)
- 2 (2010-10-27 (水) 14:02:02)
- 3 (2010-11-02 (火) 20:23:55)
- 4 (2010-12-12 (日) 23:22:36)
- 5 (2012-12-26 (水) 06:31:27)
- 6 (2013-05-21 (火) 04:18:52)
- 7 (2013-05-21 (火) 17:46:04)
- 8 (2014-02-13 (木) 11:02:37)
- 9 (2014-10-25 (土) 23:53:36)
- 10 (2014-11-13 (木) 18:07:02)
- 11 (2015-01-13 (火) 11:23:01)
- 12 (2015-03-09 (月) 10:14:45)
- 13 (2017-01-25 (水) 19:01:39)
- 14 (2017-06-29 (木) 16:53:53)
- 15 (2017-11-02 (木) 15:32:16)
- 16 (2018-02-24 (土) 19:51:30)
- 17 (2018-07-01 (日) 00:27:40)
- 18 (2018-08-13 (月) 16:39:05)
- 19 (2018-08-13 (月) 17:40:32)
- 20 (2019-08-08 (木) 18:56:04)
- 21 (2021-04-07 (水) 11:09:40)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: JTableのセルの高さを自動調整 tags: [JTable, JTextArea, TableCellRenderer] author: aterai pubdate: 2010-10-25T14:24:03+09:00 description: JTableのセルの高さを、文字列の折り返しに応じて自動調整します。 --- * 概要 [#pe4a7c92] `JTable`のセルの高さを、文字列の折り返しに応じて自動調整します。 #download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTH4TWFB1I/AAAAAAAAAR4/8C89wEJ8EUA/s800/AutoWrapTableCell.png) * サンプルコード [#f1f7d94f] #code(link){{ private List<List<Integer>> rowAndCellHeightList= new ArrayList<>(); private void adjustRowHeight(JTable table, int row, int column) { //int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth(); //int cWidth = table.getCellRect(row, column, false).width; //セルの内余白は含めない //setSize(new Dimension(cWidth, 1000)); //注目 setBounds(table.getCellRect(row, column, false)); //もしくはsetBoundsを使用する setBounds(table.getCellRect(row, column, false)); //setSizeの代わりに、setBoundsでも可 //doLayout(); //必要なさそう int preferredHeight = getPreferredSize().height; while (rowAndCellHeightList.size() <= row) { rowAndCellHeightList.add(new ArrayList<Integer>(column)); } List<Integer> cellHeightList = rowAndCellHeightList.get(row); while (cellHeightList.size() <= column) { cellHeightList.add(0); } cellHeightList.set(column, preferredHeight); //JDK 1.8.0: int max = cellHeightList.stream().max(Integer::compare).get(); int max = preferredHeight; for (int h: cellHeightList) { max = Math.max(h, max); } if (table.getRowHeight(row) != max) { table.setRowHeight(row, max); } } }} * 解説 [#cef4d3e6] 上記のサンプルでは、セルレンダラーに、`setLineWrap(true)`とした`JTextArea`を使用し、カラムサイズの変更などがあったときに、その`JTextArea`の高さを取得して`JTable#setRowHeight(int)`メソッドで各行の高さとして設定しています。 * 参考リンク [#xfcd757b] - [http://tips4java.wordpress.com/2008/10/26/text-utilities/ Text Utilities « Java Tips Weblog] - [[JTableのセル幅で文字列を折り返し>Swing/TableCellRenderer]] * コメント [#y52d6f46] #comment - 高さが微妙に更新されない場合がある…。 -- &user(aterai); &new{2010-10-27 (水) 14:02:02}; - フレームのサイズ(`JTable`の高さ)を微妙に調整すると、スクロールバーが表示・非表示を繰り返す場合がある。 -- &user(aterai); &new{2010-11-02 (火) 20:23:55}; - %%上二つの原因は同じだと思うけど、今のところ何が問題なのか分からず、お手上げ状態です。%% -- &user(aterai); &new{2010-11-02 (火) 20:26:21}; - レンダラーに以下のようなコードを追加して調整するサンプルを発見: [http://blog.botunge.dk/post/2009/10/09/JTable-multiline-cell-renderer.aspx JTable multiline cell renderer] -- &user(aterai); &new{2013-05-21 (火) 04:18:52}; -- この方法だと、セルを描画する毎に、行の高さを調整するようになるので、これまでのドラッグでリサイズ中の微妙なサイズは無くなるはず(多少重くなるかもしれないけど、違いは全く感じられない)。 -- &user(aterai); &new{2013-05-21 (火) 18:43:54}; #comment