Swing/TreeRowHeightCellMargin のバックアップ(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/TreeRowHeightCellMargin へ行く。
- 1 (2012-07-16 (月) 23:28:29)
- 2 (2012-12-08 (土) 06:54:06)
- 3 (2013-01-28 (月) 02:05:19)
- 4 (2015-01-06 (火) 16:50:28)
- 5 (2015-03-03 (火) 14:15:14)
- 6 (2016-12-21 (水) 17:58:06)
- 7 (2017-12-08 (金) 16:13:39)
- 8 (2019-12-04 (水) 19:35:30)
- 9 (2021-06-08 (火) 11:24:03)
- 10 (2025-01-03 (金) 08:57:02)
- 11 (2025-01-03 (金) 09:01:23)
- 12 (2025-01-03 (金) 09:02:38)
- 13 (2025-01-03 (金) 09:03:21)
- 14 (2025-01-03 (金) 09:04:02)
- category: swing
folder: TreeRowHeightCellMargin
title: JTreeのノードアイコンサイズとテキストの選択状態
tags: [JTree, TreeCellRenderer, Margin, Icon, JLabel]
author: aterai
pubdate: 2012-07-16T23:28:29+09:00
description: JTreeのノードアイコンによって行の高さが変更された場合のテキストの選択状態を修正します。
image:
概要
JTree
のノードアイコンによって行の高さが変更された場合のテキストの選択状態を修正します。
Screenshot

Advertisement
サンプルコード
class CompoundTreeCellRenderer extends DefaultTreeCellRenderer {
private final JPanel p = new JPanel(new BorderLayout());
private final JLabel icon = new JLabel();
private final JLabel text = new JLabel();
private final Border innerBorder = BorderFactory.createEmptyBorder(1, 2, 1, 2);
private final Border emptyBorder = BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(1, 1, 1, 1), innerBorder);
private final Border hasFocusBorder;
public CompoundTreeCellRenderer() {
super();
Color bsColor = getBorderSelectionColor();
Color focusBGColor = new Color(~getBackgroundSelectionColor().getRGB());
hasFocusBorder = BorderFactory.createCompoundBorder(
new DotBorder(focusBGColor, bsColor), innerBorder);
icon.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
text.setBorder(emptyBorder);
text.setOpaque(true);
p.setOpaque(false);
p.add(icon, BorderLayout.WEST);
JPanel wrap = new JPanel(new GridBagLayout());
wrap.setOpaque(false);
wrap.add(text);
p.add(wrap);
}
@Override public Component getTreeCellRendererComponent(
JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
JLabel l = (JLabel) super.getTreeCellRendererComponent(
tree, value, selected, expanded, leaf, row, hasFocus);
Color bColor, fColor;
if (selected) {
bColor = getBackgroundSelectionColor();
fColor = getTextSelectionColor();
} else {
bColor = getBackgroundNonSelectionColor();
fColor = getTextNonSelectionColor();
if (bColor == null) bColor = getBackground();
if (fColor == null) fColor = getForeground();
}
text.setForeground(fColor);
text.setBackground(bColor);
text.setBorder(hasFocus ? hasFocusBorder : emptyBorder);
text.setText(l.getText());
icon.setIcon(l.getIcon());
return p;
}
@Override public void paint(Graphics g) {}
}
View in GitHub: Java, Kotlin解説
- 左:
Default
UIManager.put("Tree.closedIcon", icon)
などで、JTree
のノードアイコンを変更- ノードを選択した場合、アイコン(行)の高さまで選択色で塗られる
- 右:
Label
Default
と同じノードアイコンを設定TreeCellRenderer
が各行の高さを決定するよう、JTree#setRowHeight(0)
を設定- アイコン用とテキスト用の
JLabel
を配置したJPanel
を用意し、これを使用するTreeCellRenderer
を設定- ノードが選択された場合、アイコン(行)の高さではなく、テキスト用の
JLabel
のみ背景を選択色で塗る - ノードを複数連続して選択すると行間に余白ができる
- ノードが選択された場合、アイコン(行)の高さではなく、テキスト用の