Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code for JTree with checkbox

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You can use the TreeModel, TreeCellRenderer and TreeCellEditor.
 
mahesh gaonkar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
THAT I KNOW ,BUT I WANT THE CODE FOR THAT,PLEAE SEND ME A CODE
ANY REPLY APPERCIATED
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
mahesh gaonkar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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();
}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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:

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
...
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please read what Maneesh said.
Locking this one.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic