Swing/TreeLineColor のバックアップソース(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TreeLineColor へ行く。
--- category: swing folder: TreeLineColor title: JTreeのノード間の接続線の色を変更する tags: [JTree, LookAndFeel, UIManager, BasicStroke] author: aterai pubdate: 2017-07-03T14:32:43+09:00 description: JTreeのノード間の接続線の色、太さなどを変更します。 image: https://drive.google.com/uc?export=view&id=1btj2ocoJQdwg8t8Smv1O29xUUFsfjbR5zA --- * 概要 [#summary] `JTree`のノード間の接続線の色、太さなどを変更します。 #download(https://drive.google.com/uc?export=view&id=1btj2ocoJQdwg8t8Smv1O29xUUFsfjbR5zA) * サンプルコード [#sourcecode] #code(link){{ UIManager.put("Tree.paintLines", Boolean.TRUE); UIManager.put("Tree.lineTypeDashed", Boolean.TRUE); UIManager.put("Tree.line", Color.GREEN); UIManager.put("Tree.hash", Color.RED); JTree tree0 = new JTree(); JTree tree1 = new JTree(); tree1.putClientProperty("JTree.lineStyle", "Horizontal"); JTree tree2 = new JTree(); tree2.setUI(new BasicTreeUI() { private final Stroke horizontalLine = new BasicStroke(2f); private final Stroke verticalLine = new BasicStroke(5f); @Override public Color getHashColor() { return Color.BLUE; } @Override protected void paintHorizontalPartOfLeg( Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) { Graphics2D g2 = (Graphics2D) g.create(); g2.setStroke(horizontalLine); super.paintHorizontalPartOfLeg( g2, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf); g2.dispose(); } @Override protected void paintVerticalPartOfLeg( Graphics g, Rectangle clipBounds, Insets insets, TreePath path) { Graphics2D g2 = (Graphics2D) g.create(); g2.setStroke(verticalLine); super.paintVerticalPartOfLeg(g2, clipBounds, insets, path); g2.dispose(); } }); }} * 解説 [#explanation] - `lineTypeDashed` -- `UIManager.put("Tree.lineTypeDashed", Boolean.TRUE);`を設定して接続線を点線に変更 -- `UIManager.put("Tree.hash", Color.RED);`で線色を変更可能 - `lineStyle` -- `tree1.putClientProperty("JTree.lineStyle", "Horizontal");`を設定して親ノードの上下に水平線を表示するスタイルに変更 --- 参考: [[JTreeのノード間の接続線のスタイルを変更する>Swing/TreeLineStyle]] -- `UIManager.put("Tree.line", Color.GREEN);`で水平線の線色を変更可能 --- 接続線の線色には影響しない - `BasicTreeUI` -- `BasicTreeUI#paintHorizontalPartOfLeg(...)`、`BasicTreeUI#paintVerticalPartOfLeg(...)`をオーバーライドして、接続線の太さを変更 -- 接続線の色は、`BasicTreeUI#getHashColor()`をオーバーライドして変更 -- `UIManager.put("Tree.lineTypeDashed", Boolean.TRUE);`の設定などは無視される * 参考リンク [#reference] - [[JTreeのノード間の接続線のスタイルを変更する>Swing/TreeLineStyle]] * コメント [#comment] #comment #comment