---
category: swing
folder: TreeCellMargin
title: JTreeのノードの文字列に余白を追加
tags: [JTree, TreeCellRenderer, Border, JLabel]
author: aterai
pubdate: 2010-05-17T14:59:42+09:00
description: JTreeのノードにある文字列の左右に余白を追加します。
image: https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTWBZNFbLI/AAAAAAAAAok/8_0YqLhhqTk/s800/TreeCellMargin.png
---
* 概要 [#summary]
`JTree`のノードにある文字列の左右に余白を追加します。

#download(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTWBZNFbLI/AAAAAAAAAok/8_0YqLhhqTk/s800/TreeCellMargin.png)

* サンプルコード [#sourcecode]
#code(link){{
class MyTreeCellRenderer2 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;
  private final TreeCellRenderer renderer;

  public MyTreeCellRenderer2(TreeCellRenderer renderer) {
    super();
    this.renderer = renderer;
    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);
    p.add(text);
  }
  @Override public Component getTreeCellRendererComponent(
      JTree tree, Object value, boolean selected, boolean expanded,
      boolean leaf, int row, boolean hasFocus) {
    JLabel l = (JLabel) renderer.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;
  }
}
}}

* 解説 [#explanation]
- 左
-- `Default`
- 中
-- `DefaultTreeCellRenderer#paint(...)`メソッドをオーバーライドして、余白を追加
-- 注: `WindowsLookAndFeel`(フォーカスのボーダーがアイコンの周りには描画されない)で、`getComponentOrientation().isLeftToRight()==true`の場合のみ対応
- 右
-- `DefaultTreeCellRenderer#getTreeCellRendererComponent(...)`メソッドをオーバーライドして、アイコンと文字列を別の`JLabel`に分解し、文字列にのみ`BorderFactory.createEmptyBorder(0, 0, 0, 2)`で余白を追加
-- 注: `WindowsLookAndFeel`(フォーカスのボーダーがアイコンの周りには描画されない)で、`getComponentOrientation().isLeftToRight()==true`の場合のみ対応

//* 参考リンク [#reference]
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/tree/DefaultTreeCellRenderer.html DefaultTreeCellRenderer (Java Platform SE 8 )]

* コメント [#comment]
#comment
#comment