这一切都来自记忆。如果我明天发现有任何问题,我会编辑。
查看 BasicTreeUI 或 MetalTreeUI。我相信他们都画线。
您需要做的是创建一个新类,该类扩展(我在这里猜测名称)SubstanceTreeUI 并覆盖paintHorizontalPartOfLeg() 和paintVerticalPartOfLeg()。然后你有一个选择:
- 您可以调用 myTree.setUI( new MyTreeUI() )
- UIManager.getDefaults().put("TreeUI", MyTreeUI.class.getName() ) 在您制作 JTree 之前的某个时间点
如果您不想子类化,请尝试 UIManager.getDefaults().put("TreeUI", BasicTreeUI.class.getName() ) 看看是否可以。
编辑 2:经过进一步审查,在创建树之前调用 JTree 上的 .setUI(new BasicTreeUI()) 或调用 UIManager.getDefaults().put("TreeUI", BasicTreeUI.class.getName() ) 会更容易.
编辑:
SubstanceTreeUI 是 BasicTreeUI 的子类。它已经覆盖了paintXXXPartOfLeg()。
水平:
@Override
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
Insets insets, Rectangle bounds, TreePath path, int row,
boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
boolean toPaint = (!this.tree.isEnabled())
|| this.isInside
|| !FadeConfigurationManager.getInstance().fadeAllowed(
SubstanceLookAndFeel.TREE_DECORATIONS_ANIMATION_KIND,
tree);
if (FadeTracker.getInstance().isTracked(this.tree, SubstanceLookAndFeel.TREE_DECORATIONS_ANIMATION_KIND)) {
Graphics2D graphics = (Graphics2D) g.create();
graphics.setComposite(TransitionLayout
.getAlphaComposite(this.tree,
FadeTracker.getInstance()
.getFade10(this.tree,SubstanceLookAndFeel.TREE_DECORATIONS_ANIMATION_KIND) / 10.0f,
g));
super.paintHorizontalPartOfLeg(graphics, clipBounds, insets,
bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
graphics.dispose();
} else if (toPaint) {
super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds, path,
row, isExpanded, hasBeenExpanded, isLeaf);
}
}
看起来只有以下任一情况为真时才会绘制线条:
- 树未启用,鼠标位于树边界 [this.inside] 并且树上不允许淡入 (?) [!FadeConfigurationManager.getInstance().fadeAllowed(...)]
- FadeTracker 正在跟踪 JTree [FadeTracker.getInstance().isTracked(...)]
弄清楚如何确保 FadeTracker 正在跟踪 JTree,或者尝试这个非常粗略的 hack(见下文):
您还可以将 BasicTreeUI 的 paintXXXPartOfLeg 方法中的代码剪切并粘贴到子类中。
public MyTree extends JTree {
private boolean overrideIsEnable = false;
public void setOverrideIsEnable(boolean b) { overrideIsEnabeld=true; }
public boolean isOverrideIsEnable(boolean b) { return overrideIsEnabeld; }
public boolean isEnabled() {
if(overrideIsEnabled) return false;
return super.isEnabled();
}
}
class MyTreeUI extends SubstanceTreeUI {
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
Insets insets, Rectangle bounds, TreePath path, int row,
boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
if(this.tree instanceof MyTree)
try {
Field f = SubstanceTreeUI.class.getDeclaredField("inside");
f.setAccessible(true);
Boolean v = (Boolean)f.get(this);
f.set(this,Boolean.true);
((MyTree)this.tree).setOverrideIsEnable(true);
} catch(Throwable t) {
//want to default back to substanceUI if this fails.
}
super.paintHoriz.....();
try{
f.set(this,v);
((MyTree)this.tree).setOverrideIsEnable(true);
}catch(Throwable t) {
//see above
}
}
//repeat for Vertical
}