java JTree JCheckBox树复选框详解

网友投稿 378 2023-03-14


java JTree JCheckBox树复选框详解

本文实例为大家分享了java JTree JCheckBox树复选框展示的具体代码,供大家参考,具体内容如下

1.CheckTreeManager.java

public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener

{

private CheckTreeSelectionModel selectionModel = null;

// private JTree tree = new JTree();

private JTree tree = null;

nmsyqLOnU 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.nmsyqLOnUremoveSelectionPath(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

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

{

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

{

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

TreePath path = paths[i];

TreePath[] selectionPaths = getSelectionPaths();

if(selectionPaths==null)

break;

ArrayList toBeRemoved = new ArrayList();

for(int j = 0; 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

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

{

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

{

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

TreePath path = paths[i];

TreePath[] selectionPaths = getSelectionPaths();

if(selectionPaths==null)

break;

ArrayList toBeRemoved = new ArrayList();

for(int j = 0; 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

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

{

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

TreePath path = paths[i];

TreePath[] selectionPaths = getSelectionPaths();

if(selectionPaths==null)

break;

ArrayList toBeRemoved = new ArrayList();

for(int j = 0; 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

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

TreePath path = paths[i];

TreePath[] selectionPaths = getSelectionPaths();

if(selectionPaths==null)

break;

ArrayList toBeRemoved = new ArrayList();

for(int j = 0; 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

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

{

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

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

{

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(parentNohttp://de);

for(int i = 0; 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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

{

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

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

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

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);

Object childNode = model.getChild(node, i);

if(childNode!=peekNode)

super.addSelectionPaths(new TreePath[]{temp.pathByAddingChild(childNode)});

}

}

super.removeSelectionPaths(new TreePath[]{parent});

}

}

3.CheckTreeCellRenderer .java

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 rendernmsyqLOnUer = 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.用法

CheckTreeManager checkTreeManager = new CheckTreeManager(jTree);


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:网关和api网关(api网关设计原则)
下一篇:Java工作队列代码详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~