Swing/TwoRowsCellRenderer のバックアップの現在との差分(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TwoRowsCellRenderer へ行く。
- 1 (2010-12-20 (月) 15:44:39)
- 2 (2012-12-25 (火) 11:18:38)
- 3 (2013-10-25 (金) 20:29:41)
- 4 (2014-06-12 (木) 21:41:49)
- 5 (2014-11-01 (土) 00:42:27)
- 6 (2015-10-22 (木) 00:34:07)
- 7 (2015-10-26 (月) 19:51:41)
- 8 (2017-04-19 (水) 15:46:54)
- 9 (2017-11-02 (木) 15:26:29)
- 10 (2019-05-22 (水) 19:35:38)
- 11 (2019-05-24 (金) 18:22:59)
- 12 (2019-07-24 (水) 18:57:43)
- 13 (2021-03-26 (金) 11:08:02)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのセル内に二行だけ表示 #navi(../) #tags(JTable, TableCellRenderer, JLabel, JPanel) RIGHT:Posted by &author(aterai); at 2010-12-20 * JTableのセル内に二行だけ表示 [#fde22d74] `JTable`のセル内に文字列をニ行分だけ表示し、あふれる場合は"..."で省略します。 --- category: swing folder: TwoRowsCellRenderer title: JTableのセル内に二行だけ表示 tags: [JTable, TableCellRenderer, JLabel, JPanel] author: aterai pubdate: 2010-12-20T15:44:39+09:00 description: JTableのセル内に文字列を二行分だけ表示し、あふれる場合は...で省略します。 image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQ77KlEsZJI/AAAAAAAAAuE/mc9fcp-ZmBU/s800/TwoRowsCellRenderer.png --- * 概要 [#summary] `JTable`のセル内に文字列を二行分だけ表示し、あふれる場合は`...`で省略します。 - &jnlp; - &jar; - &zip; #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQ77KlEsZJI/AAAAAAAAAuE/mc9fcp-ZmBU/s800/TwoRowsCellRenderer.png) #ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQ77KlEsZJI/AAAAAAAAAuE/mc9fcp-ZmBU/s800/TwoRowsCellRenderer.png) ** サンプルコード [#z0aafd98] * サンプルコード [#sourcecode] #code(link){{ JTable table = new JTable(model); table.setAutoCreateRowSorter(true); table.setRowHeight(table.getRowHeight() * 2); table.setDefaultRenderer(String.class, new TwoRowsCellRenderer()); }} // ... #code{{ class TwoRowsCellRenderer extends JPanel implements TableCellRenderer { JLabel top = new JLabel(); JLabel bottom = new JLabel(); private final JLabel top = new JLabel(); private final JLabel bottom = new JLabel(); public TwoRowsCellRenderer() { super(new GridLayout(2,1,0,0)); super(new GridLayout(2, 1, 0, 0)); add(top); add(bottom); } @Override public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int column) { @Override public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } setFont(table.getFont()); FontMetrics fm = table.getFontMetrics(table.getFont()); String text = (value==null) ? "" : value.toString(); String first = text; String second = ""; int columnWidth = table.getColumnModel().getColumn(column).getWidth(); int textWidth = 0; for(int i=0; i<text.length(); i++) { String text = Objects.toString(value, ""); String first = text; String second = ""; // int columnWidth = table.getColumnModel().getColumn(column).getWidth(); int columnWidth = table.getCellRect(0, column, false).width; int textWidth = 0; for (int i = 0; i < text.length(); i++) { textWidth += fm.charWidth(text.charAt(i)); if(textWidth>columnWidth) { first = text.substring(0,i-1); second = text.substring(i-1); if (textWidth > columnWidth) { first = text.substring(0, i - 1); second = text.substring(i - 1); break; } } top.setText(first); bottom.setText(second); return this; } } }} ** 解説 [#g15d8b79] `JLabel`を上下に配置した`JPanel`を使って、`TableCellRenderer`を作成しています。"..."での省略は、二行目の`JLabel`のデフォルト動作です。 * 解説 [#explanation] `JLabel`を上下に配置した`JPanel`を使って、`TableCellRenderer`を作成しています。`...`での省略は、セル内`2`行目で使用している`JLabel`のデフォルト動作です。 ** 参考リンク [#c9d954f9] - 補助文字(サロゲートペアなど)を含む文字列を扱う場合は、`String#charAt(int)`ではなく`String#codePointAt(int)`や`Character.charCount(codePoint)`などを使用する必要がある -- [https://www.ibm.com/developerworks/jp/ysl/library/java/j-unicode_surrogate/index.html Java による Unicode サロゲートプログラミング] #code{{ int i = 0; while (i < text.length()) { int cp = text.codePointAt(i); textWidth += fm.charWidth(cp); if (textWidth > columnWidth) { first = text.substring(0, i); second = text.substring(i); break; } i += Character.charCount(cp); } }} * 参考リンク [#reference] - [[JLabelの文字列を折り返し>Swing/GlyphVector]] - [https://www.ibm.com/developerworks/jp/ysl/library/java/j-unicode_surrogate/index.html Java による Unicode サロゲートプログラミング] - [https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-line-clamp -webkit-line-clamp - CSS: Cascading Style Sheets | MDN] ** コメント [#l2f449cc] * コメント [#comment] #comment #comment