• Post Reply 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

How to checked CheckBox in JTree only when user click on CheckBox?

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my application I am showing JCheckBox with every node of JTree. My requirement is JCheckBox should be selected or deselected only when user click on JCheckBox not by clicking on the Node. Means if I click the text part of node it should not select or deselect the JCheckBox. Also i need to handel the node text part selection separately.
I have seen various sites like CodeGuru etc. for getting JCheckBox with JTree, but they have not provided the solution of my problem. So is ther any way to achieve what I want?

Please provide me any sample code for this problem, if anyone has.

Thanks in advance.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. But in this code instead of the textfield i need a label and i should be able to select it whenever it is clicked. Selecting the node should not tick/untick the checkbox. I need to populate the selected node with somme more child nodes, only when the node text is clicked. But if i click the checkbox nither the node should be populated with child nodes nor the corresponding label should not be selected.
Basically the implementation should: checkbox and the node selection work wherever the mouse is clicked.

Here is the modified code.
Please give me some solution soon. I am struggling with this requirement from quite a long time.

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

public class SplitNodeTest {
private JScrollPane getContent() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new SplitNode("Root", false, ".//images//folder.gif"));

root.add(new DefaultMutableTreeNode(new SplitNode("Node 1", false, ".//images//folder.gif")));
root.add(new DefaultMutableTreeNode(new SplitNode("Node 2", true, ".//images//folder.gif")));

JTree tree = new JTree(new DefaultTreeModel(root));
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 SplitNode {
String name;
boolean value;
String img;

public SplitNode(String s, boolean b, String img) {
this.name = s;
this.value = b;
this.img = img;
}

public String toString() {
return "SplitNode[value: " + value + ", name:" + name + "]";
}
}

class SplitNodeRenderer implements TreeCellRenderer {
JLabel label;
JLabel imgLabel;
JCheckBox checkBox;
JLabel textField;
JPanel panel;
public String imgname;

public SplitNodeRenderer() {
label = new JLabel();
imgLabel = new JLabel();
checkBox = new JCheckBox();
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
textField = new JLabel();
//textField.setEditable(false);
textField.setBackground(UIManager.getColor("Tree.background"));
textField.setBorder(null);
panel = new JPanel();
panel.setOpaque(false);
panel.add(checkBox);
panel.add(imgLabel);
panel.add(textField);
}

public Component getTreeCellRendererComponent(JTree tree,
Object value,
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(splitNode.value);
textField.setText(splitNode.name);

imgname = splitNode.img;
// imgLabel.setIcon(new ImageIcon(".//TreefolderImage//folder.gif"));

return panel;
} else {
label.setText(node.toString());
return label;
}
}
}

class SplitNodeEditor extends AbstractCellEditor
implements TreeCellEditor, ActionListener {
JLabel label;
JLabel imgLabel;
JCheckBox checkBox;
JLabel textField;
SplitNode splitNode;
JComponent editedComponent;
JPanel panel;
String imgname;

public SplitNodeEditor() {
label = new JLabel();
imgLabel = new JLabel();
checkBox = new JCheckBox();
checkBox.addActionListener(this);
checkBox.setBackground(UIManager.getColor("Tree.background"));
checkBox.setBorder(null);
checkBox.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
checkBox.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
}
public void mouseReleased(MouseEvent e) {
checkBox.setBorder(null);
}
});
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(imgLabel);
panel.add(textField);
}

public Component getTreeCellEditorComponent(JTree tree,
Object value,
boolean isSelected,
boolean expanded,
boolean leaf,
int row) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
if(node.getUserObject() instanceof SplitNode) {
splitNode = (SplitNode)node.getUserObject();
checkBox.setSelected(splitNode.value);
textField.setText(splitNode.name);

imgname = splitNode.img;
// imgLabel.setIcon(new ImageIcon(".//TreefolderImage//folder.gif"));

return panel;
} else {
label.setText(node.toString());
return label;
}
}

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

public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
Point p = ((MouseEvent)anEvent).getPoint();
JTree tree = (JTree)anEvent.getSource();
TreePath path = tree.getPathForLocation(p.x, p.y);
DefaultMutableTreeNode node = (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();
}
}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in this code instead of the textfield i need a label and i should be able to select
it whenever it is clicked.

I tried using a JLabel at first but could not get it to show up in the node. So I tried
the JTextField and it seems to work okay.
Selecting the node should not tick/untick the checkbox.
You can select the checkBox by clicking on it or you can select the text/textField by
clicking on it. Clicking on either one does not change the other.
I need to populate the selected node with somme more child nodes, only when the node
text is clicked.

You can do this now; click on the string and type.
But if i click the checkbox nither the node should be populated with child nodes nor
the corresponding label should not be selected.

This is the way it is set up to work.

Edit the checkBox by clicking on the checkBox. The edit session ends as soon as you click
on the checkBox.
You can edit the string in the node by clicking on the string/textField. After clicking on
the string you will notice a blinking caret in the textField. You are then ready to edit
the string. When you finish editing the string press the enter key - the edit session will
end.
I haven't looked at the modified code yet.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was easier with the Icon and JLabel. Editing navigation is the same.
reply
    Bookmark Topic Watch Topic
  • New Topic