一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java JTree JCheckBox樹復選框詳解

java JTree JCheckBox樹復選框詳解

2021-02-19 23:17yyyzlf Java教程

這篇文章主要為大家詳細介紹了java JTree JCheckBox樹復選框的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java JTree JCheckBox復選框展示的具體代碼,供大家參考,具體內容如下

1.CheckTreeManager.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener
{
 
  private CheckTreeSelectionModel selectionModel = null;
  
// private JTree tree = new JTree();
  private JTree tree = null;
  
  int hotspot = new JCheckBox().getPreferredSize().width;
  
  public CheckTreeManager(JTree tree)
  {
   this.tree = tree;
   selectionModel = new CheckTreeSelectionModel(tree.getModel());
   tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel));
   tree.addMouseListener(this); //鼠標監聽
   selectionModel.addTreeSelectionListener(this); //樹選擇監聽
  }
  
  public void mouseClicked(MouseEvent me)
  {
   TreePath path = tree.getPathForLocation(me.getX(), me.getY());
   if(path==null)
    return;
   if(me.getX()>tree.getPathBounds(path).x+hotspot)
    return;
   
   boolean selected = selectionModel.isPathSelected(path, true);
   selectionModel.removeTreeSelectionListener(this);
   
   try
   {
    if(selected)
     selectionModel.removeSelectionPath(path);
    else
     selectionModel.addSelectionPath(path);
   }
   finally
   {
    selectionModel.addTreeSelectionListener(this);
    tree.treeDidChange();
   }
  }
  
  public CheckTreeSelectionModel getSelectionModel()
  {
   return selectionModel;
  }
  
  public void valueChanged(TreeSelectionEvent e)
  {
   tree.treeDidChange();
  }
  
}

2.CheckTreeSelectionModel.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
public class CheckTreeSelectionModel extends DefaultTreeSelectionModel
{
  private TreeModel model;
  
  public CheckTreeSelectionModel(TreeModel model)
  {
   this.model = model;
   setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
  }
  
  // tests whether there is any unselected node in the subtree of given path
  public boolean isPartiallySelected(TreePath path){
   if(isPathSelected(path, true))
    return false;
   TreePath[] selectionPaths = getSelectionPaths();
   if(selectionPaths==null)
    return false;
   for(int j = 0; j<selectionPaths.length; j++)
   {
    if(isDescendant(selectionPaths[j], path))
     return true;
   }
   return false;
  }
  
  // tells whether given path is selected.
  // if dig is true, then a path is assumed to be selected, if
  // one of its ancestor is selected.
  public boolean isPathSelected(TreePath path, boolean dig)
  {
   if(!dig)
    return super.isPathSelected(path);
   while(path!=null && !super.isPathSelected(path))
    path = path.getParentPath();
   return path!=null;
  }
  
  // is path1 descendant of path2
  private boolean isDescendant(TreePath path1, TreePath path2)
  {
   Object obj1[] = path1.getPath();
   Object obj2[] = path2.getPath();
   for(int i = 0; i<obj2.length; i++)
   {
    if(obj1[i]!=obj2[i])
     return false;
   }
   return true;
  }
  
  public void setSelectionPaths(TreePath[] pPaths)
  {
   throw new UnsupportedOperationException("not implemented yet!!!");
  }
  
  public void addSelectionPaths(TreePath[] paths)
  {
   // unselect all descendants of paths[]
   for(int i = 0; i<paths.length; i++){
    TreePath path = paths[i];
    TreePath[] selectionPaths = getSelectionPaths();
    if(selectionPaths==null)
     break;
    ArrayList toBeRemoved = new ArrayList();
    for(int j = 0; j<selectionPaths.length; j++)
    {
     if(isDescendant(selectionPaths[j], path))
      toBeRemoved.add(selectionPaths[j]);
    }
    super.removeSelectionPaths((TreePath[])toBeRemoved.toArray(new TreePath[0]));
   }
   
   // if all siblings are selected then unselect them and select parent recursively
   // otherwize just select that path.
   for(int i = 0; i<paths.length; i++)
   {
    TreePath path = paths[i];
    TreePath temp = null;
    while(areSiblingsSelected(path))
    {
     temp = path;
     if(path.getParentPath()==null)
      break;
     path = path.getParentPath();
    }
    if(temp!=null)
    {
     if(temp.getParentPath()!=null)
      addSelectionPath(temp.getParentPath());
     else
     {
      if(!isSelectionEmpty())
       removeSelectionPaths(getSelectionPaths());
      super.addSelectionPaths(new TreePath[]{temp});
     }
    }
    else
     super.addSelectionPaths(new TreePath[]{ path});
   }
  }
  
  // tells whether all siblings of given path are selected.
  private boolean areSiblingsSelected(TreePath path)
  {
   TreePath parent = path.getParentPath();
   if(parent==null)
    return true;
   Object node = path.getLastPathComponent();
   Object parentNode = parent.getLastPathComponent();
   
   int childCount = model.getChildCount(parentNode);
   for(int i = 0; i<childCount; i++)
   {
    Object childNode = model.getChild(parentNode, i);
    if(childNode==node)
     continue;
    if(!isPathSelected(parent.pathByAddingChild(childNode)))
     return false;
   }
   return true;
  }
  
  public void removeSelectionPaths(TreePath[] paths)
  {
   for(int i = 0; i<paths.length; i++){
    TreePath path = paths[i];
    if(path.getPathCount()==1)
     super.removeSelectionPaths(new TreePath[]{ path});
    else
     toggleRemoveSelection(path);
   }
  }
  
  // if any ancestor node of given path is selected then unselect it
  // and selection all its descendants except given path and descendants.
  // otherwise just unselect the given path
  private void toggleRemoveSelection(TreePath path)
  {
   Stack stack = new Stack();
   TreePath parent = path.getParentPath();
   while(parent!=null && !isPathSelected(parent))
   {
    stack.push(parent);
    parent = parent.getParentPath();
   }
   if(parent!=null)
    stack.push(parent);
   else{
    super.removeSelectionPaths(new TreePath[]{path});
    return;
   }
   
   while(!stack.isEmpty())
   {
    TreePath temp = (TreePath)stack.pop();
    TreePath peekPath = stack.isEmpty() ? path : (TreePath)stack.peek();
    Object node = temp.getLastPathComponent();
    Object peekNode = peekPath.getLastPathComponent();
    int childCount = model.getChildCount(node);
    for(int i = 0; i<childCount; i++){
     Object childNode = model.getChild(node, i);
     if(childNode!=peekNode)
      super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});
    }
   }
   super.removeSelectionPaths(new TreePath[]{parent});
  }
  
  
}

3.CheckTreeCellRenderer .java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer
{
 private CheckTreeSelectionModel selectionModel;
 private TreeCellRenderer delegate;
// private TristateCheckBox checkBox = new TristateCheckBox();
 private JCheckBox checkBox = new JCheckBox();
 
 public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
  this.delegate = delegate;
  this.selectionModel = selectionModel;
  setLayout(new BorderLayout());
  setOpaque(false);
  checkBox.setOpaque(false);
 }
 
 
 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
  Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
 
  TreePath path = tree.getPathForRow(row);
  if(path!=null)
  {
   System.out.println(path);
   if(selectionModel.isPathSelected(path, true))
    checkBox.setSelected(true);
   else
   {
    System.out.println(selectionModel.isPartiallySelected(path));
    checkBox.setSelected(selectionModel.isPartiallySelected(path) ? true : false);
   }
  }
  removeAll();
  add(checkBox, BorderLayout.WEST);
  add(renderer, BorderLayout.CENTER);
  return this;
 }
}

4.用法

?
1
CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/yyyzlf/article/details/4410565

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产剧情中文视频在线 | 色琪琪久久se色 | 日韩乱淫| 白丝美女用胸伺候主人 | 精品免费tv久久久久久久 | 日韩aⅴ在线观看 | 91国内在线国内在线播放 | 成人影音先锋 | 男男gaygays国内 | 国产精品女同久久免费观看 | 99久热只有精品视频免费看 | 双性受合不垅腿攻np | 日本小视频网站 | 久久精品国产清白在天天线 | 色老板最新网站视频地址 | 国内亚州视频在线观看 | 舔到喷水| 精品国产自在在线在线观看 | 九九九国产在线 | 久久天天躁狠狠躁夜夜躁 | 国产在线步兵一区二区三区 | 果冻传媒 天美 麻豆 | 欧式午夜理伦三级在线观看 | 国语对白做受xxxx | 出轨同学会免费观看 | 天天爽天天干天天操 | 成人免费高清视频 | 91麻豆在线观看 | 国产亚洲精品看片在线观看 | 日本在线播放 | 人人看人人射 | 亚色九九九全国免费视频 | 久青草国产在线观看视频 | 精品国产91久久久久久久a | 西西人体大胆77777视频 | 欧美靠逼 | 91制片厂制作果冻传媒破解 | 4455在线 | 亚洲精品中文字幕久久久久久 | 国产一卡 | 福利视频一区二区思瑞 |