aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Checkbox with Jtree 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
Bookmark "Checkbox with Jtree" Watch "Checkbox with Jtree" New topic
Author

Checkbox with Jtree

mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
Please see the code and tell me what is worng in it, i am not able to check the multiple checkbox at a time


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,root1));
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;
}
public String toString()
{
return "SplitNode[value: " + value + ", name:" + name+ "]";
}
}
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);
//TreePath path = tree.getSelectionPath();
if (path != null && node1 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(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();
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
return to the originating post of the code and go through this method line by line
getTreeCellRendererComponent()

you have made a change that causes your problem
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
please tell me what is wrong in above code,i gone though the code line by line but i am unable to get the solution.please tel me it is urgent
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
checkBox.setSelected(selected);
checkBox.setSelected(splitNode.value);
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
In above code if i click on node with checkbox ,i want all the checkbox with leaf should be selected which beloning to that node and present node.please tell me how to do it.requirement is urgent.

[ May 31, 2007: Message edited by: mahesh gaonkar ]

[ May 31, 2007: Message edited by: mahesh gaonkar ]
[ May 31, 2007: Message edited by: mahesh gaonkar ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add a TreeSelectionListener
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
please tell me i want to add treeselection listener to checkbox or jtree
I tried with mouselistener,i can able get the above requiremtn,but the problem is when i select the label check is also selected .please tell me the solution.

[ June 01, 2007: Message edited by: mahesh gaonkar ]
[ June 01, 2007: Message edited by: mahesh gaonkar ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> please tell me i want to add treeselection listener to checkbox or jtree

start here
http://java.sun.com/docs/books/tutorial/uiswing/events/treeselectionlistener.html
mahesh gaonkar
Greenhorn

Joined: May 23, 2007
Posts: 11
please tell me what is wromg in this code ,i added the mouse listener to tree to select leaf checkbox beloning to the node.my problem is when click on label checkbox is selected and deselected,i donot want checkbox should be selected when clcik on label

code

import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
import java.awt.*;
import javax.swing.event.*;

public class SplitNodeTest
{
public JTree Tree=null;
private JScrollPane getContent()
{
DefaultMutableTreeNoderoot = new DefaultMutableTreeNode("Root");
SplitNoderoot1 = new SplitNode("Node 1", false);
SplitNoderoot2 = 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)));
Tree= new JTree(newDefaultTreeModel(root));

Tree.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e) {
System.out.println("path");
//TreePath path = null;
//path = Tree.getSelectionPath();
int x = e.getX();
int y = e.getY();
int row = Tree.getRowForLocation(x, y);

TreePath path = Tree.getPathForRow(row);
System.out.println("path"+path);
//TreePath path = Tree.getSelectionPath();
//DefaultMutableTreeNode node = (DefaultMutableTreeNode)
// Tree.getLastSelectedPathComponent();
//Tree.getSelectionModel().setSelectionMode
//(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
if(path != null)
{
SplitNode node1=null;
DefaultMutableTreeNode node = (DefaultMutableTreeNode)Tree.getLastSelectedPathComponent();
// if(path.getLastPathComponent() instanceof DefaultMutableTreeNode)
//{

if(Tree.getLastSelectedPathComponent() instanceof SplitNode)
{
node1 = (SplitNode)path.getLastPathComponent();
boolean isSelected = !(node1.isSelected());
node1.setSelected(isSelected);
((DefaultTreeModel)Tree.getModel()).nodeChanged(node1);
}
}
//}
}




//DefaultMutableTreeNode node = (DefaultMutableTreeNode)
// Tree.getLastSelectedPathComponent();

/* if nothing is selected */
//if (node == null) return;

/* retrieve the node that was selected */
//Object nodeInfo = node.getUserObject();

/* React to the node selection. */
//}
}
);
Tree.setEditable(true);
Tree.setCellRenderer(new SplitNodeRenderer());
Tree.setCellEditor(new SplitNodeEditor());
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;
boolean IsSelected = false;
String pidstring;
//SplitNode node=null;
public SplitNode(Objecto, boolean isSelected)
{
super(o);
this.name = o.toString();
this.IsSelected =isSelected;
//this.value = isSelected;
}
public SplitNode(Objecto, boolean isSelected,String pidstring)
{
super(o);
this.name = o.toString();
this.IsSelected =isSelected;
this.value = isSelected;
this.pidstring = pidstring;
}

public void setSelected(boolean isSelected)
{
this.IsSelected = isSelected;

if (children != null)
{
Enumeration enum = children.elements();

while (enum.hasMoreElements()) {
SplitNode node = (SplitNode)enum.nextElement();
System.out.println(node);
node.setSelected(isSelected);
}
}
}


public boolean isSelected() {
return IsSelected;
}

}

class SplitNodeRenderer implementsTreeCellRenderer
{
JLabel label;
JCheckBox checkBox;
JTextField textField;
JPanel panel;
JTree Tree;

public SplitNodeRenderer()
{
label = new JLabel();
//this.Tree=tree;
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;
String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
//this.Tree = tree;

if(value instanceof SplitNode)
{
SplitNode splitNode = (SplitNode)value;
checkBox.setSelected(splitNode.IsSelected);
//checkBox.setSelected(splitNode.isSelected());
//checkBox.setSelected(splitNode.isSelected());
textField.setText(stringValue);
return panel;
}
else
{
label.setText(stringValue);
return label;
}


/*Tree.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
click(event);
}
});*/

}

}
class SplitNodeEditor extends AbstractCellEditor implements TreeCellEditor, ActionListener
{
JLabellabel;
JCheckBoxcheckBox;
JLabeltextField;
SplitNodesplitNode;
JComponent editedComponent;
JPanelpanel;
JTree Tree=null;

//Object value1;
public SplitNodeEditor()
{
label = new JLabel();
//this.Tree = tree;
checkBox = new JCheckBox();
//checkBox.addActionListener(this);
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
//checkBox.addMouseListener(new NodeSelectionListener(tree));
// checkBox.a
// Simple st = new Simple();
// st.Tree = tree;

// value1= value;


textField = new JLabel();
// 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;
//String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row);
//System.out.println(node);

if(value instanceof SplitNode)
{
splitNode = (SplitNode)value;
System.out.println("splitnode"+splitNode.pidstring);
checkBox.setSelected(splitNode.IsSelected);
//checkBox.setSelected(splitNode.isSelected());
//checkBox.setSelected(splitNode.isSelected());
textField.setText(splitNode.name);
String id = splitNode.pidstring;
System.out.println("id in applet"+id);
System.out.println("servlet output in applet");

//RootObject robj = new RootObject();
//robj.setRoot(id);

/*String location = "http://localhost:9082/flick/servlet/ImageServlet";

try
{
Viewer acrobat = new Viewer();
URL image_location = new URL(location);
URLConnection servletConnection = image_location.openConnection();
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setDoOutput(true);
OutputStream output = servletConnection.getOutputStream();
ObjectOutputStream os = new ObjectOutputStream(output);
os.writeObject(robj);
InputStream input = servletConnection.getInputStream();
System.out.println("connected");
}
catch(Exception ex)
{
ex.printStackTrace();
}*/
return panel;
}
else
{
label.setText(splitNode.name);
return label;
}
}
public boolean isCellEditable(EventObject anEvent)
{

return true;
}

public void actionPerformed(ActionEvent e)
{
editedComponent =(JComponent)e.getSource();
super.stopCellEditing();
}

/* (non-Javadoc)
* @see javax.swing.CellEditor#getCellEditorValue()
*/

public Object getCellEditorValue()
{
if(editedComponent == textField)
splitNode.name = textField.getText();
//else
// splitNode.value = checkBox.isSelected();
returnsplitNode;
}

/* (non-Javadoc)
* @see javax.swing.tree.TreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int)
*/
/* (non-Javadoc)
* @see javax.swing.tree.TreeCellEditor#getTreeCellEditorComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int)
*/
/*public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
// TODO Auto-generated method stub
return this;
}*/
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the original code doesn't have your current problem.

I'd say the best way to find the problem is to start from scratch, making your
changes only one at a time, recompiling/testing after each change.

Eventually you should find the change that is causing the problem.
 
wood burning stoves
 
subject: Checkbox with Jtree