2012-10-13 41 views

Respuesta

15

Este es un ejemplo completo que muestra cómo agregar una casilla de verificación al nodo Jtree. Usé JTree con nodos basados ​​en el contenido del sistema de archivos.

Usé también la clase AddCheckBoxToTree.CheckTreeManager para administrar las opciones de selección o semi selección.

Uso

public AddCheckBoxToTree.CheckTreeManager getCheckTreeManager() { 
    return checkTreeManager; 
} 

método para jugar con el camino de árbol de selección.

por ejemplo:

// clear all selected path in order 
    TreePath[] paths=getCheckTreeManager().getSelectionModel().getSelectionPaths(); 
    if(paths != null){ 
     for(TreePath tp : paths){ 
      getCheckTreeManager().getSelectionModel().removeSelectionPath(tp); 
     } 
    } 

aquí me pegan todo el código que hacen eso:

package com.demo.tree.checkbox; 


import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.event.MouseEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
import java.io.File; 
import java.util.Vector; 

import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTree; 
import javax.swing.SwingUtilities; 
import javax.swing.ToolTipManager; 
import javax.swing.UIManager; 
import javax.swing.event.TreeExpansionEvent; 
import javax.swing.event.TreeExpansionListener; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.TreeCellRenderer; 
import javax.swing.tree.TreePath; 
import javax.swing.tree.TreeSelectionModel; 



public class FileTreeViewer extends JFrame { 

private static final long serialVersionUID = 1L; 
public static final ImageIcon ICON_COMPUTER = new ImageIcon(""); 
public static final ImageIcon ICON_DISK = new ImageIcon("defaults1.png"); 
public static final ImageIcon ICON_FOLDER = new ImageIcon("fol_orig.png"); 
public static final ImageIcon ICON_EXPANDEDFOLDER = new ImageIcon("folder_open.png"); 

protected JTree m_tree; 
protected DefaultTreeModel m_model; 

AddCheckBoxToTree AddCh = new AddCheckBoxToTree(); 

private AddCheckBoxToTree.CheckTreeManager checkTreeManager; 


protected TreePath m_clickedPath; 

public FileTreeViewer() 
{ 
    super("Demo tree check box"); 
    setSize(400, 300); 

    DefaultMutableTreeNode top = new DefaultMutableTreeNode(
      new IconData(ICON_COMPUTER, null, "Computer")); 

    DefaultMutableTreeNode node; 
    File[] roots = File.listRoots(); 
    for (int k=0; k<roots.length; k++) 
    { 
     node = new DefaultMutableTreeNode(new IconData(ICON_DISK, null, new FileNode(roots[k]))); 
     top.add(node); 
     node.add(new DefaultMutableTreeNode(new Boolean(true))); 
    } 

    m_model = new DefaultTreeModel(top); 

    m_tree = new JTree(m_model){ 
     public String getToolTipText(MouseEvent ev) 
     { 
      if(ev == null) 
       return null; 
      TreePath path = m_