TITLE:JTreeの選択背景色を変更
Posted by at 2012-10-29

JTreeの選択背景色を変更

JTreeのノード条件によって、その選択背景色を変更します。
  • category: swing folder: TreeBackgroundSelectionColor title: JTreeの選択背景色を変更 tags: [JTree, TreeCellRenderer] author: aterai pubdate: 2012-10-29T01:14:10+09:00 description: JTreeのノード条件によって、その選択背景色を変更します。 image: https://lh4.googleusercontent.com/-7JA4jpNa55U/UI1VhdHlkwI/AAAAAAAABVw/dAUHGh4q014/s800/TreeBackgroundSelectionColor.png

概要

JTreeのノード条件によって、その選択背景色を変更します。
TreeBackgroundSelectionColor.png

サンプルコード

サンプルコード

class SelectionColorTreeCellRenderer extends DefaultTreeCellRenderer {
  private Color color = null;
#spanadd

#spanend
  private void setParticularCondition(Object value) {
    if (value instanceof DefaultMutableTreeNode) {
      Object uo = ((DefaultMutableTreeNode) value).getUserObject();
      if (uo instanceof Color) {
        color = (Color) uo;
        return;
      }
    }
    color = null;
  }
#spanadd

#spanend
  @Override public Component getTreeCellRendererComponent(
      JTree tree, Object value, boolean isSelected, boolean expanded,
      boolean leaf, int row, boolean hasFocus) {
    JComponent c = (JComponent)super.getTreeCellRendererComponent(
    JComponent c = (JComponent) super.getTreeCellRendererComponent(
        tree, value, isSelected, expanded, leaf, row, hasFocus);
    if(isSelected) {
    if (isSelected) {
      setParticularCondition(value);
      c.setForeground(getTextSelectionColor());
      c.setBackground(getBackgroundSelectionColor());
      if(leaf && value.toString().startsWith("a")) {
      if (leaf && value.toString().startsWith("a")) {
        c.setOpaque(true);
        c.setBackground(Color.RED);
      }else{
      } else {
        c.setOpaque(false);
        c.setBackground(getBackgroundSelectionColor());
      }
    }else{
    } else {
      c.setForeground(getTextNonSelectionColor());
      c.setBackground(getBackgroundNonSelectionColor());
    }
    return c;
  }
  private Color color = null;
  private void setParticularCondition(Object value) {
    if(value instanceof DefaultMutableTreeNode) {
      Object uo = ((DefaultMutableTreeNode)value).getUserObject();
      if(uo instanceof Color) {
        color = (Color)uo;
        return;
      }
    }
    color = null;
  }
#spanadd

#spanend
  @Override public Color getBackgroundSelectionColor() {
    return color!=null ? color : super.getBackgroundSelectionColor();
    return color != null ? color : super.getBackgroundSelectionColor();
  }
}
View in GitHub: Java, Kotlin

解説

上記のサンプルでは、以下の条件でノードの選択背景色を変更しています。

解説

上記のサンプルでは、以下の条件でノードの選択時背景色を変更しています。
  • DefaultMutableTreeNode#getUserObject()がColorの場合、その色を選択背景色に変更
    • DefaultTreeCellRenderer#getBackgroundSelectionColor()をオーバーライド
    • ノードアイコンの背景は選択状態にならず、ノードテキストの背景色のみ変更される
  • ノードテキストが "a" で始まる場合、選択背景色をColor.REDに変更
    • TreeCellRenderer#getTreeCellRendererComponent(...)で取得したコンポーネント(=JLabel)をsetOpaque(true)で不透明、setBackground(Color.RED)で背景色を赤に変更
  • DefaultMutableTreeNode#getUserObject()Colorの場合、その色を選択時背景色に採用
    • DefaultTreeCellRenderer#getBackgroundSelectionColor()をオーバーライド
    • ノードアイコンの背景は選択状態に変更せずノードテキストの背景色のみ変更
  • ノードテキストがaで始まる場合、選択背景色をColor.REDに変更
    • TreeCellRenderer#getTreeCellRendererComponent(...)で取得したコンポーネント(JLabel)をsetOpaque(true)で不透明、setBackground(Color.RED)で背景色を変更
    • ノードアイコン、テキストの背景色が共に選択状態になる
    • SynthLookAndFeelで作成されているNimbusLookAndFeelなどでは上記のような選択背景色は適用されない
Nimbus などの Look & Feel では、上記のような選択背景色にならない場合があります。

参考リンク

コメント

コメント