File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Code for JTree with checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

Code for JTree with checkbox

mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
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
You can use the TreeModel, TreeCellRenderer and TreeCellEditor.


The Zen of Java Programming.
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
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: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
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
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: 19722
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Iban Cardona
Greenhorn

Joined: Apr 15, 2009
Posts: 9
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
...


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
sonia arora
Ranch Hand

Joined: Mar 10, 2010
Posts: 31
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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3168
    
  47

Please read what Maneesh said.
Locking this one.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Code for JTree with checkbox