Swing/DifferentCellHeight のバックアップの現在との差分(No.14)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/DifferentCellHeight へ行く。
- 1 (2006-05-15 (月) 09:36:24)
- 2 (2007-05-22 (火) 13:58:48)
- 3 (2011-09-30 (金) 17:33:25)
- 4 (2012-08-09 (木) 23:55:40)
- 5 (2012-09-10 (月) 12:35:02)
- 6 (2013-03-07 (木) 15:42:25)
- 7 (2013-10-18 (金) 16:01:43)
- 8 (2014-09-18 (木) 02:42:12)
- 9 (2014-09-27 (土) 02:08:35)
- 10 (2014-11-22 (土) 03:59:58)
- 11 (2015-01-16 (金) 21:17:07)
- 12 (2015-03-09 (月) 14:46:02)
- 13 (2015-03-16 (月) 17:28:33)
- 14 (2015-08-21 (金) 18:37:32)
- 15 (2016-12-17 (土) 18:38:38)
- 16 (2017-03-29 (水) 13:59:15)
- 17 (2018-02-20 (火) 15:27:36)
- 18 (2020-01-31 (金) 15:08:23)
- 19 (2021-07-29 (木) 03:56:43)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: DifferentCellHeight title: JListで異なる高さのセルを使用 tags: [JList, JTextArea, ListCellRenderer] author: aterai pubdate: 2006-05-15T09:36:24+09:00 description: JListのレンダラーにJTextAreaを使って、異なる高さのセルを作成します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTK2Z8UOTI/AAAAAAAAAWo/7GoDkuVX8Fc/s800/DifferentCellHeight.png --- * 概要 [#v5f5cc63] * 概要 [#summary] `JList`のレンダラーに`JTextArea`を使って、異なる高さのセルを作成します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTK2Z8UOTI/AAAAAAAAAWo/7GoDkuVX8Fc/s800/DifferentCellHeight.png) * サンプルコード [#r38aaded] * サンプルコード [#sourcecode] #code(link){{ class TextAreaRenderer extends JTextArea implements ListCellRenderer<String> { private Border focusBorder; private static final Border NOMAL_BORDER = BorderFactory.createEmptyBorder(2, 2, 2, 2); class TextAreaRenderer<E extends String> extends JTextArea implements ListCellRenderer<E> { private static final Color EVEN_COLOR = new Color(230, 255, 230); private Border noFocusBorder; private Border focusBorder; @Override public Component getListCellRendererComponent( JList list, String str, int index, JList<? extends E> list, E value, int index, boolean isSelected, boolean cellHasFocus) { //setLineWrap(true); setText(Objects.toString(str, "")); // setLineWrap(true); setText(Objects.toString(value, "")); if (isSelected) { setBackground(list.getSelectionBackground()); // Nimbus setBackground(new Color(list.getSelectionBackground().getRGB())); setForeground(list.getSelectionForeground()); } else { setBackground(index % 2 == 0 ? EVEN_COLOR : list.getBackground()); setForeground(list.getForeground()); } if (cellHasFocus) { if (focusBorder == null) { focusBorder = new DotBorder( new Color(~list.getSelectionBackground().getRGB()), 2); } setBorder(focusBorder); } else { setBorder(NOMAL_BORDER); setBorder(noFocusBorder); } return this; } @Override public void updateUI() { super.updateUI(); focusBorder = UIManager.getBorder("List.focusCellHighlightBorder"); noFocusBorder = UIManager.getBorder("List.noFocusBorder"); if (Objects.isNull(noFocusBorder) && Objects.nonNull(focusBorder)) { Insets i = focusBorder.getBorderInsets(this); noFocusBorder = BorderFactory.createEmptyBorder( i.top, i.left, i.bottom, i.right); } } } private DefaultListModel makeList() { DefaultListModel model = new DefaultListModel(); model.addElement("一行"); model.addElement("一行目\n二行目"); model.addElement("一行目\n二行目\n三行目"); model.addElement("四行\n以上ある\nテキスト\nの場合"); return model; } }} * 解説 [#deb3ece5] 左が複数行に対応した`JList`、右が通常の`JList`になります。左の`JList`では、`JList#getFixedCellHeight()`が`-1`で、`ListCellRenderer`に`JTextArea`を使用しているため、テキストに`\n`を含めることで複数行を作成することができます。 * 解説 [#explanation] - 右: デフォルトの`JList` - 左: 複数行に対応した`JList` -- `JList#getFixedCellHeight()`が`-1`で`ListCellRenderer`に`JTextArea`を使用しているため、テキストに`\n`を含めることで複数行が表示可能 - セルの選択状態 -- `UIManager.getBorder("List.focusCellHighlightBorder")`を使用するように変更 セルの区切りを分かりやすくするために、偶数奇数で行の背景色を変更しています。 `JTextArea`にセルフォーカスがある状態を表現するために、`LineBorder`を継承して作成した`DotBorder`を使用しています。 #code{{ class DotBorder extends LineBorder { public DotBorder(Color color, int thickness) { super(color, thickness); } @Override public boolean isBorderOpaque() { return true; } @Override public void paintBorder( Component c, Graphics g, int x, int y, int w, int h) { Graphics2D g2 = (Graphics2D) g; g2.translate(x, y); g2.setPaint(getLineColor()); BasicGraphicsUtils.drawDashedRect(g2, 0, 0, w, h); g2.translate(-x, -y); } } }} * 参考リンク [#l382633c] * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JList.html#setFixedCellHeight-int- JList#setFixedCellHeight(int) (Java Platform SE 8)] * コメント [#h396eb78] * コメント [#comment] #comment #comment