Author
Code for JTree with checkbox
mahesh gaonkar
Greenhorn
Joined: May 23, 2007
Posts: 11
posted May 27, 2007 08:59:00
0
i want the example of Jtree with checkbox,first requirement is checkbox should be selected only when click on it not on label,and second requirement i want multiple checkbox be selected on mouse click on checkbox,any sloution shoud be apperciated [ May 27, 2007: Message edited by: mahesh gaonkar ] [ May 27, 2007: Message edited by: mahesh gaonkar ]
Pravin Jain
Ranch Hand
Joined: May 20, 2007
Posts: 60
posted May 27, 2007 11:22:00
0
You can use the TreeModel , TreeCellRenderer and TreeCellEditor .
The Zen of Java Programming.
mahesh gaonkar
Greenhorn
Joined: May 23, 2007
Posts: 11
posted May 27, 2007 11:48:00
0
THAT I KNOW ,BUT I WANT THE CODE FOR THAT,PLEAE SEND ME A CODE ANY REPLY APPERCIATED
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
posted May 27, 2007 13:42:00
0
Welcome to JavaRanch. I think you misunderstand what we do here at JavaRanch. We try to help people learn Java, not provide them with ready-made solutions to their problems. If you are stuck in your implementation, post the code you have so far, and indicate what it does or does not do. Then we can help you get going again. And please, KeepItDown .
Android apps – ImageJ plugins – Java web charts
mahesh gaonkar
Greenhorn
Joined: May 23, 2007
Posts: 11
posted May 28, 2007 08:28:00
0
Please tell me what is wrong in this code.problem is i am not able check the multiple checkbox at a time code import java.awt.*; import java.awt.event.*; import java.util.EventObject ; import javax.swing.*; import javax.swing.tree.*; import java.util.*; public class SplitNodeTest { private JScrollPane getContent() { DefaultMutableTreeNoderoot = new DefaultMutableTreeNode ("Root"); DefaultMutableTreeNoderoot1 = new DefaultMutableTreeNode (new SplitNode("Node 1", false)); DefaultMutableTreeNoderoot2 = new DefaultMutableTreeNode (new SplitNode("Node 2", false)); root1.add(root2); root.add(root1); // root.add(new DefaultMutableTreeNode (new SplitNode("Node 1", false))); // root.add(new DefaultMutableTreeNode (new SplitNode("Node 2", true))); JTree tree= new JTree (newDefaultTreeModel(root)); tree.setEditable(true); tree.setCellRenderer(new SplitNodeRenderer()); tree.setCellEditor(new SplitNodeEditor(tree,root)); return new JScrollPane (tree); } public static void main(String [] args) { JFrame f = new JFrame (); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new SplitNodeTest().getContent()); f.setSize(360,300); f.setLocation(200,200); f.setVisible(true); } } class SplitNodeextends DefaultMutableTreeNode { Stringname; boolean value; protected boolean isSelected; public SplitNode(Strings, boolean isSelected) { name = s; this.isSelected =isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; if (children != null) { Enumeration enum = children.elements(); while (enum.hasMoreElements()) { CheckNode node = (CheckNode)enum.nextElement(); node.setSelected(isSelected); } } } public boolean isSelected() { return isSelected; } } class SplitNodeRenderer implementsTreeCellRenderer { JLabel label; JCheckBox checkBox; JTextField textField; JPanel panel; public SplitNodeRenderer() { label = new JLabel (); checkBox = new JCheckBox (); checkBox.setBackground(UIManager.getColor("Tree.background")); checkBox.setBorder(null); textField =newJTextField(); textField.setEditable(false); textField.setBackground(UIManager.getColor("Tree.background")); textField.setBorder(null); panel = new JPanel (); panel.setOpaque(false); panel.add(checkBox); panel.add(textField); } public Component getTreeCellRendererComponent(JTree tree,Objectvalue,boolean selected, boolean expanded, boolean leaf,int row,boolean hasFocus) { DefaultMutableTreeNode node =(DefaultMutableTreeNode )value; if(node.getUserObject() instanceof SplitNode) { SplitNode splitNode = (SplitNode)node.getUserObject(); checkBox.setSelected(selected); textField.setText(splitNode.name); return panel; } else { label.setText(node.toString()); return label; } } } class SplitNodeEditor extends AbstractCellEditor implements TreeCellEditor , ActionListener { JLabellabel; JCheckBoxcheckBox; JTextFieldtextField; SplitNodesplitNode; JComponent editedComponent; JPanelpanel; final JTree Tree; Object value1; public SplitNodeEditor(JTree tree,Object value) { label = new JLabel (); checkBox = new JCheckBox (); checkBox.addActionListener(this); checkBox.setBackground(UIManager.getColor("Tree.background")); checkBox.setBorder(null); this.Tree = tree; value1= value; checkBox.addMouseListener(new MouseAdapter () { public void mousePressed(MouseEvent e) { System.out.println("hello"); int x = e.getX(); int y = e.getY(); int row = Tree.getRowForLocation(x, y); TreePath path = Tree.getPathForRow(1); // System.out.println(path.getLastPathComponent()); DefaultMutableTreeNodenode1 = (DefaultMutableTreeNode )value1; //if(node.getUserObject() instanceofSplitNode) //System.out.println(node1.getUserObject()); //TreePath path = tree.getSelectionPath(); if (path != null && node1.getUserObject() instanceof SplitNode) { SplitNode node = (SplitNode)path.getLastPathComponent(); boolean isSelected = ! (node.isSelected()); System.out.println(isSelected); node.setSelected(isSelected); ((DefaultTreeModel ) Tree.getModel()).nodeChanged(node); } } publicvoid mouseReleased(MouseEvent e) { checkBox.setBorder(null); } }); textField = new JTextField (); textField.addActionListener(this); textField.setBackground(UIManager.getColor("Tree.background")); textField.setBorder(null); panel = new JPanel (); panel.setOpaque(false); panel.add(checkBox); panel.add(textField); } public Component getTreeCellEditorComponent(JTreetree,Objectvalue,boolean isSelected, boolean expanded,boolean leaf,int row) { DefaultMutableTreeNodenode = (DefaultMutableTreeNode )value; if(node.getUserObject() instanceofSplitNode) { splitNode =(SplitNode)node.getUserObject(); checkBox.setSelected(splitNode.isSelected()); textField.setText(splitNode.name); return panel; } else { label.setText(node.toString()); return label; } } public Object getCellEditorValue() { if(editedComponent == textField) splitNode.name = textField.getText(); else splitNode.value = checkBox.isSelected(); returnsplitNode; } public boolean isCellEditable(EventObject anEvent) { if (anEventinstanceof MouseEvent ) { /*Point p= ((MouseEvent )anEvent).getPoint(); JTree tree =(JTree )anEvent.getSource(); TreePathpath = tree.getPathForLocation(p.x,p.y); DefaultMutableTreeNodenode =(DefaultMutableTreeNode )path.getLastPathComponent(); int clickCountToStart=(node.getUserObject() instanceof SplitNode) ? 1 : 2;*/ //return((MouseEvent )anEvent).getClickCount() >= clickCountToStart; } return true; } public void actionPerformed(ActionEvent e) { editedComponent =(JComponent )e.getSource(); super.stopCellEditing(); } }
Jonathan McBride
Greenhorn
Joined: Jul 14, 2009
Posts: 1
posted Jul 14, 2009 11:09:48
0
Your problem is right here:
mahesh gaonkar wrote:
public Component getTreeCellRendererComponent(
JTree tree,Objectvalue,
boolean selected , boolean expanded,
boolean leaf,int row,boolean hasFocus)
{
...
checkBox.setSelected(selected);
...
"Selected" means whether or not this is the last node that the user clicked on with his/her mouse. Only one node can be "selected" at a time. When you check a different node, the previously-selected node gets unselected, and therefore unchecked. If you want selection to toggle between checked/unchecked, do this instead:
Or, if you're feeling particularly clever/devious:
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
posted Jul 14, 2009 12:43:58
0
I don't think Mahesh is going to read this - he hasn't been active here in over 2 years.
http://faq.javaranch.com/java/DontWakeTheZombies
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Iban Cardona
Greenhorn
Joined: Apr 15, 2009
Posts: 9
posted Sep 01, 2009 09:06:47
0
I need to do something like Mahesh. I will look into this post and I will try to implement a JTree with checkboxes.
Rob, I'm the same as the JTree-Zip. Now my next step is to implement the JTree with checkboxes and unzip the selected items.
mark goking
Ranch Hand
Joined: Aug 18, 2009
Posts: 155
posted Oct 09, 2009 02:54:27
0
...
Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
posted Oct 09, 2009 03:23:48
0
Mark,
I suppose you did not see http://faq.javaranch.com/java/DontWakeTheZombies which Rob indicated.
You can start a post your question in a new thread by clicking on the
[Donate a pint, save a life! ] [How to ask questions ] [Onff-turn it on! ]
sonia arora
Ranch Hand
Joined: Mar 10, 2010
Posts: 31
posted Mar 10, 2010 23:46:45
0
i need to implement a JTree with checkboxes as nodes and want to add a event handler on each checkbox node selection.
can someone help.
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
posted Mar 11, 2010 00:05:59
0
Please read what Maneesh said.
Locking this one.
Author of ExamLab (Download ) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
subject: Code for JTree with checkbox