我有一個Jtree和2個按鈕來選擇和取消選擇所有節點。我做了這樣的嘗試:JTree:以編程方式選擇所有節點
selectAll = new JButton("Select all");
selectAll.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int row = 0;
while (row < curvesTree.getRowCount())
{
curvesTree.expandRow(row);
row++;
}
int entradasTree = curvesTree.getRowCount();
for(int i=0; i<entradasTree; i++){
TreePath path = curvesTree.getPathForRow(i);
curvesTree.setSelectionPath(path);
}
}
});
unselectAll = new JButton("Unselect all");
unselectAll.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
curvesTree.clearSelection();
}
});
非選擇按鈕似乎是工作,但全選只擴大JTree的,並選擇最後一個節點。我認爲每次以編程方式選擇一個節點時,我都不選擇前一個節點。
JTree的配置是這樣的:
curvesTree = new JTree(rootNode);
curvesTree.setExpandsSelectedPaths(true);
curvesTree.getSelectionModel().setSelectionMode(TreeSelectionModel.
DISCONTIGUOUS_TREE_SELECTION);
外觀極好抓,你怎麼會...... +1 – mKorbel
謝謝,我一直在半上午尋找一個方法:) –