[英]How to add gaps to a JTree
我的問題是這個
我正在嘗試創建一個並排顯示兩棵樹的組件。 這兩棵樹通常非常相似,但可能會有一兩個不同之處。 如果存在差異,即一個分支有一個分支,而另一個沒有分支,我希望沒有分支的樹為它顯示一個空白空間,也為它丟失的每個孩子顯示一個空白空間。
所以它可能看起來像
left tree right tree ------------ ------------- + Root + Root | | --Child A --Child A | | --Child B | | | --Child C --Child C
使用自定義渲染器刪除那些應該是間隙的行的文本和圖標是相對直接的。 但是,這仍然留下了將孩子連接到父母的垂直線的水平線。 這是我的問題。
left tree right tree ------------ ------------- + Root + Root | | --Child A --Child A | | --Child B -- <--I want to remove this | | --Child C --Child C
在這個簡單的例子中它可能是可以裸露的,但是當丟失的分支也有孩子時,我最終會得到很多連接間隙的小線。
我認為一個潛在的替代方法是為每棵樹創建一個列 JTreeTable 並為缺少的分支清空單元格。 雖然這將意味着垂直線的一部分也將丟失。
任何幫助、想法或評論都會非常有用。 謝謝。
考慮以下:
class ReticentTreeUI extends BasicTreeUI {
private Set<Integer> hiddenRows = new HashSet<Integer>();
public void hideRow(int row) {
hiddenRows.add(row);
}
@Override
protected void paintHorizontalPartOfLeg(Graphics g,
Rectangle clipBounds, Insets insets, Rectangle bounds,
TreePath path, int row, boolean isExpanded,
boolean hasBeenExpanded, boolean isLeaf) {
if (!hiddenRows.contains(row)) {
super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds,
path, row, isExpanded, hasBeenExpanded, isLeaf);
}
}
@Override
protected void paintRow(Graphics g, Rectangle clipBounds,
Insets insets, Rectangle bounds, TreePath path, int row,
boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
if (!hiddenRows.contains(row)) {
super.paintRow(g, clipBounds, insets, bounds, path, row,
isExpanded, hasBeenExpanded, isLeaf);
}
}
@Override
protected void paintExpandControl(Graphics g, Rectangle clipBounds,
Insets insets, Rectangle bounds, TreePath path, int row,
boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
if (!hiddenRows.contains(row)) {
super.paintExpandControl(g, clipBounds, insets, bounds,
path, row, isExpanded, hasBeenExpanded, isLeaf);
}
}
}
用法示例:
JTree tree = new JTree();
ReticentTreeUI ui = new ReticentTreeUI();
tree.setUI(ui);
ui.hideRow(2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.