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.Renderer code: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; } }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; }


Reply With Quote


Bookmarks