Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Rendering VS Selection in JTrees

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Rendering VS Selection in JTrees

    Hi all,

    I'm new to this forum, and I hope we'll cooperate, always.

    I'm using a custom cell renderer for jtrees that renders leaf nodes to checkboxes. This was obtained, yet once rendering is OK, I cannot select more than one item of the tree. Thus, rendering is hindering the properties of the default selection model of the tree.

    Any help to apply the selection properties with rendering?

    Below is my code:

    [I]import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.EventObject;

    import javax.swing.AbstractCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.TreeExpansionEvent;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.event.TreeWillExpandListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.ExpandVetoException;
    import javax.swing.tree.TreeCellEditor;
    import javax.swing.tree.TreeCellRenderer;
    import javax.swing.tree.TreeSelectionModel;


    public class CheckBoxNodeTreeSample implements TreeWillExpandListener {

    CheckBoxNode accessNode, browseNode, dummyNode, rootNode;
    JTree tree;

    CheckBoxNode accessibilityOptions[] = {
    new CheckBoxNode("Access 1"),
    new CheckBoxNode("Access 2")
    };

    CheckBoxNode browsingOptions[] = { new CheckBoxNode("Browse 1"),
    new CheckBoxNode("Browse 2"),
    new CheckBoxNode("Browse 3"),
    new CheckBoxNode("Browse 4")
    };

    public static void main(String args[]) {
    new CheckBoxNodeTreeSample();
    }

    public CheckBoxNodeTreeSample(){
    final JFrame frame = new JFrame("CheckBox Tree");

    browseNode = new CheckBoxNode("Browsing");
    accessNode = new CheckBoxNode("Accessibility");
    rootNode = new CheckBoxNode("Root");
    dummyNode = new CheckBoxNode("Dummy Node");

    createInitialTree();
    tree = new JTree(rootNode);

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);

    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);
    tree.addTreeWillExpandListener(this);
    tree.setShowsRootHandles(true);

    tree.getSelectionModel().setSelectionMode(TreeSele ctionModel.DISCONTIGUOUS_TREE_SELECTION); //.SINGLE_TREE_SELECTION);

    tree.getSelectionModel().addTreeSelectionListener( new TreeSelectionListener(){
    public void valueChanged(TreeSelectionEvent se) {
    if (tree.getSelectionRows()!=null)
    printIt(tree.getSelectionRows());

    DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if(selectedNode!=null){
    String selectedNodeName = selectedNode.toString();
    System.out.println(selectedNodeName);
    }
    }

    public void printIt (int a[]){
    System.out.print("Selected Nodes: ");

    for(int counter=0; counter<a.length; counter++)
    System.out.print(a[counter]+" ");
    }
    });

    tree.addMouseListener(new MouseAdapter(){

    public void mouseClicked(MouseEvent arg0) {
    if (arg0.getX()>frame.getWidth()-150 && arg0.getY()>frame.getHeight()-150){
    System.out.println(arg0.getX());
    System.out.println(arg0.getY());
    }
    }
    });

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setBounds(500, 300, 500, 300);
    frame.setVisible(true);
    }

    public void createInitialTree(){
    rootNode.add(browseNode);
    rootNode.add(accessNode);
    browseNode.add(dummyNode);
    dummyNode = new CheckBoxNode("Dummy Node");
    accessNode.add(dummyNode);
    }

    public void treeWillCollapse(TreeExpansionEvent arg0) throws ExpandVetoException {
    if(arg0.getPath().toString().endsWith(accessNode.t oString()+"]")){
    System.out.println("Accessibility Collapsed.");
    accessNode.removeAllChildren();
    dummyNode = new CheckBoxNode("Dummy Node");
    accessNode.add(dummyNode);
    }
    else if(arg0.getPath().toString().endsWith(browseNode.t oString()+"]")){
    System.out.println("Browsing Collapsed.");
    browseNode.removeAllChildren();
    dummyNode = new CheckBoxNode("Dummy Node");
    browseNode.add(dummyNode);
    }
    }

    public void treeWillExpand(TreeExpansionEvent arg0) throws ExpandVetoException {
    if(arg0.getPath().toString().endsWith(accessNode.t oString()+"]")){
    System.out.println("Accessibility Expanded.");
    accessNode.removeAllChildren();

    for (int i=0; i<accessibilityOptions.length; i++)
    accessNode.add(accessibilityOptions[i]);
    }
    else if(arg0.getPath().toString().endsWith(browseNode.t oString()+"]")){
    System.out.println("Browsing Expanded.");
    browseNode.removeAllChildren();

    for (int i=0; i<browsingOptions.length; i++)
    browseNode.add(browsingOptions);
    }
    }
    }

    class CheckBoxNodeRenderer implements TreeCellRenderer {

    private JCheckBox leafRenderer = new JCheckBox();
    private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

    protected JCheckBox getLeafRenderer() {
    return leafRenderer;
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

    Component returnValue;
    CheckBoxNode node=null;

    if (leaf) {
    leafRenderer.setText(value+"");
    leafRenderer.setEnabled(tree.isEnabled());
    leafRenderer.setSelected(false);

    if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
    Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
    if (userObject instanceof CheckBoxNode) {
    node = (CheckBoxNode) userObject;
    leafRenderer.setText(node.getText());
    leafRenderer.setSelected(node.isSelected());
    //System.out.println("Entered Here: "+node+" isSelected: "+node.isSelected());
    }
    }
    returnValue = leafRenderer;
    } else {
    returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    }

    returnValue.setBackground(null);


    return returnValue;
    }
    }

    class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    ChangeEvent changeEvent = null;

    JTree tree;

    public CheckBoxNodeEditor(JTree tree) {
    this.tree = tree;
    }

    public Object getCellEditorValue() {
    JCheckBox checkbox = renderer.getLeafRenderer();
    CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(), checkbox.isSelected());
    return checkBoxNode;
    }

    public boolean isCellEditable(EventObject event) {
    return true;
    }

    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row){

    Component editor = renderer.getTreeCellRendererComponent(tree, value, true, expanded, leaf, row, true);

    // editor always selected / focused
    ItemListener itemListener = new ItemListener() {
    public void itemStateChanged(ItemEvent itemEvent) {
    if (stopCellEditing()) {
    fireEditingStopped();
    }
    }
    };

    if (editor instanceof JCheckBox) {
    ((JCheckBox) editor).addItemListener(itemListener);
    }

    return editor;
    }
    }

    class CheckBoxNode extends DefaultMutableTreeNode{
    String text;
    boolean selected=false;

    public CheckBoxNode(String text) {
    this.text = text;
    }

    public CheckBoxNode(String text, boolean selected) {
    this.text = text;
    this.selected=selected;
    }

    public boolean isSelected() {
    return selected;
    }

    public void setSelected(boolean newValue) {
    selected = newValue;
    }

    public String getText() {
    return text;
    }

    public void setText(String newValue) {
    text = newValue;
    }

    public String toString() {
    return "["+ text+"] "+this.getChildCount()+" child(ren)";
    }
    }



    Thanks in advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Rendering VS Selection in JTrees

    That sure is a lot of code, and it doesn't compile for me. I recommend you post an SSCCE- that means eliminate any extra code, and make sure it compiles and runs. Also, don't forget to wrap your code in highlight tags to make it more readable.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Drag and Drop in JTrees
    By helloworld922 in forum Java Swing Tutorials
    Replies: 4
    Last Post: March 21st, 2014, 11:16 AM
  2. JTrees are a nightmare!
    By Danjb in forum AWT / Java Swing
    Replies: 3
    Last Post: July 4th, 2011, 07:43 AM
  3. [SOLVED] JSF? not rendering.
    By newbie in forum Web Frameworks
    Replies: 2
    Last Post: March 21st, 2011, 08:45 PM
  4. Drag and Drop in JTrees
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: February 20th, 2010, 03:52 PM
  5. Drag and Drop in JTrees
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: January 19th, 2010, 11:51 PM

Tags for this Thread