Help required in Checkbox node Tree


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Help required in Checkbox node Tree

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    Help required in Checkbox node Tree

    Hello All,
    I am very new to Java Swing and written a program to create a Jtree with checkbox in every node. My requirement is that I should be able to select the checkbox and the node independently. With the present code, it looks that it is happening. But my issue is that when I click the checkbox on the parent, it does not check the checkboxes on its child. Also I am able to click on the checkbox on single node only. I have given below my Renderer and Editor Code.

    Any help on this is appreciated. Any sample renderer and editor code for my requirement 1) checkbox and node should be selected separately 2) Selecting the parent checkbox should select the child checkboxes as well would be great.

    Thanks
    Lavanya.
    Code:
       public Object getCellEditorValue() {
        //JCheckBox checkbox = renderer.getLeafRenderer();
        checkbox = renderer.getLeafRenderer();
        CheckBoxTreeNode checkBoxNode = new CheckBoxTreeNode(checkbox.getText(),checkbox.isSelected());
        return checkBoxNode;
      }
    
      public boolean isCellEditable(EventObject event) {
    
       System.out.println("inside iscelleditable");
       if( event instanceof MouseEvent)
       { 
          checkbox = renderer.getLeafRenderer();
          MouseEvent me = (MouseEvent) event;
          int x = me.getX();
          int y = me.getY();
          int row = tree.getRowForLocation(x, y);
          TreePath  path = tree.getPathForRow(row);
          //TreePath  path = tree.getSelectionPath();
          if (path != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
    	if(node.isRoot())
            {
              Enumeration e = node.breadthFirstEnumeration();
              int i=0;
    	  boolean sel = false;
    	  boolean childsel = false;
    	  while (e.hasMoreElements())
    	  {
    	     node = (DefaultMutableTreeNode) e.nextElement();
    	     Object obj = node.getUserObject();
    	     System.out.println(obj.toString());
    	     if(i==0) //Parent node
    	     {
    		sel = checkbox.isSelected();
    	        System.out.println("Selected:"+sel);
    		checkbox.setSelected(true);
    	     }
    	     else {
    		System.out.println("I am here");
    		checkbox.setSelected(true);
    	     }
    	     i++; 
    	  }
    	   
             }
      }
      }
       return true;
      }
    
      public Component getTreeCellEditorComponent(JTree tree, Object value,
          boolean selected, boolean expanded, boolean leaf, int row) {
    
        Component editor = renderer.getTreeCellRendererComponent(tree, value,true, expanded, leaf, row, true);
    
        ActionListener actionListener = new ActionListener() {
          public void actionPerformed(ActionEvent event) {
    	System.out.println("Inside action listener");
            if (stopCellEditing()) {
              fireEditingStopped();
            }
            if(event.getSource() == checkbox) {
              if(checkbox.isSelected()) {
                System.out.println("checkbox selected");
    	    String selStr = checkbox.getText();
    	    System.out.println(selStr);
    	    menu.runList.add(selStr);
              }
    	  else {
                System.out.println("Checkbox unselected");
    	    String tc = checkbox.getText();
    	    System.out.println(tc);
    	  }
            }
            } 	
        };
    
        if (editor instanceof JCheckBox) {
           ((JCheckBox) editor).addActionListener(actionListener);
        }
        return editor;
      }
    }
    Renderer code:
    Code:
    public Component getTreeCellRendererComponent(JTree tree, Object value,
          boolean isSelected, boolean expanded, boolean leaf, int row,
          boolean hasFocus) {
        String stringValue = tree.convertValueToText(value, isSelected,
            expanded, leaf, row, hasFocus);
        setEnabled(tree.isEnabled());
        if(value instanceof CheckNode)
        { 
         check.setSelected(((CheckNode) value).isSelected());
        }
        label.setFont(tree.getFont());
        label.setText(stringValue);
        label.setSelected(isSelected);
        label.setFocus(hasFocus);
        return this;
      }
    Last edited by Hack; 02-16-2008 at 04:20 AM. Reason: Added Code Tags

Similar Threads

  1. Naming a tree node
    By dojo23 in forum Java
    Replies: 1
    Last Post: 06-28-2005, 09:02 AM
  2. Replies: 0
    Last Post: 10-16-2002, 01:30 AM
  3. focus of a tree node
    By Fred in forum .NET
    Replies: 0
    Last Post: 07-30-2002, 04:49 AM
  4. EMERGENT.... double linked list
    By mary in forum Java
    Replies: 0
    Last Post: 07-20-2001, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center