Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 583703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:49:14+00:00 2026-05-13T14:49:14+00:00

I am attempting to modify the standard Swing JTree to intermingle nodes with and

  • 0

I am attempting to modify the standard Swing JTree to intermingle nodes with and without checkboxes. This is an example:

alt text

When I attempt to check/uncheck one of the checkboxes (the ‘User 01’ node in this example), the tree loses nodes:

alt text

I my code is an adaptation of this example: http://forums.sun.com/thread.jspa?threadID=5321084&start=13.

Instead of embedding a JCheckBox in a DefaultMutableTreeNode like this:

new DefaultMutableTreeNode(new CheckBoxNode("Accessibility", true));

I thought it made more sense to create a model node that derived from the DefaultMutableTreeNode, which I call JTreeNode. This class automatically sets the DefaultMutableTreeNode’s UserObject to a JCheckBox. The class’ ShowCheckBox property is used by the TreeCellRenderer to determine if the JCheckBox or DefaultTreeCellRenderer is used. The JTreeNode is used like this:

    JTreeNode user01 = new JTreeNode("User 01");
    user01.setShowCheckBox(true);
    user01.setSelected(true);

I believe the problem is with the class that implements the TreeCellEditor, specifically in the getCellEditorValue() or getTreeCellEditorComponent() methods. I suspect the issue has something to with the getCellEditorValue() returning a derivative of the DefaultMutableTreeNode, rather than a simpler model instance.

public Object getCellEditorValue() {

    JCheckBox checkBox = renderer.getCheckBoxRenderer();

    JTreeNode node = new JTreeNode(checkBox.getText());
    node.setShowCheckBox(true);
    node.setSelected(checkBox.isSelected());
    return node;

}

public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, 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;

}

Here is the TreeCellRender’s getTreeCellRendererComponent() method:

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

    Component component;

    //if object being passed for rendering is a JTreeNode that should show a JCheckBox, attempt to render it so
    if (((JTreeNode) value).getShowCheckBox()) {

        String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);

        //set default JCheckBox rendering
        checkBoxRenderer.setText(stringValue);
        checkBoxRenderer.setSelected(false);    //not checked
        checkBoxRenderer.setEnabled(tree.isEnabled());

        if (selected) {
            //removed colorization
            //checkBoxRenderer.setForeground(selectionForeground);
            //checkBoxRenderer.setBackground(selectionBackground);
        }
        else {
            checkBoxRenderer.setForeground(textForeground);
            checkBoxRenderer.setBackground(textBackground);
        }

        //DefaultMutableTreeNode
        if ((value != null) && (value instanceof JTreeNode)) {

            //userObject should be a JTreeNode instance
            //DefaultMutableTreeNode
            //Object userObject = ((JTreeNode) value).getUserObject();

            //if it is
            //if (userObject instanceof JTreeNode) {
                //cast as a JTreeNode
                //JTreeNode node = (JTreeNode) userObject;
                JTreeNode node = (JTreeNode) value;

                //set JCheckBox settings to match JTreeNode's settings
                checkBoxRenderer.setText(node.getText());
                checkBoxRenderer.setSelected(node.isSelected());

            //}

        }

        component = checkBoxRenderer;

    }

    //if not, render the default
    else {

        component = defaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

    }

    return component;

}

Any thoughts are greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:49:14+00:00Added an answer on May 13, 2026 at 2:49 pm

    I was able to solve the problem.

    alt text

    I created a model class (TreeNodeModel) to hold the relevant node data: key, value, hasCheckBox, isSelected:

    public class TreeNodeModel {
    
        int key;
        String value;
        boolean isSelected=false;
        boolean hasCheckBox=false;
    
        public TreeNodeModel() {
        }
        public TreeNodeModel(int key, String value) {
            this.key=key;
            this.value = value;
        }
        public TreeNodeModel(int key, String value, boolean hasCheckBox) {
            this.key=key;
            this.value = value;
            this.hasCheckBox = hasCheckBox;
        }
        public TreeNodeModel(int key, String value, boolean hasCheckBox, boolean isSelected) {
            this.key=key;
            this.value = value;
            this.hasCheckBox=hasCheckBox;
            this.isSelected = isSelected;
        }
    
        public boolean isSelected() {
            return this.isSelected;
        }
        public void setSelected(boolean newValue) {
            this.isSelected = newValue;
        }
    
        public boolean hasCheckBox() {
            return this.hasCheckBox;
        }
        public void setCheckBox(boolean newValue) {
            this.hasCheckBox=newValue;
        }
    
        public int getKey() {
            return this.key;
        }
        public void setKey(int newValue) {
            this.key = newValue;
        }
    
        public String getValue() {
            return this.value;
        }
        public void setValue(String newValue) {
            this.value = newValue;
        }
    
        @Override
        public String toString() {
            return getClass().getName() + "[" + this.value + "/" + this.isSelected + "]";
    //        return this.text;
        }
    
    }
    

    I created an implementation of the TreeCellEditor interface:

    public class TreeNodeEditor  extends AbstractCellEditor implements TreeCellEditor {
    
        private JTree tree;
        private TreeNodeModel editedModel = null;
    
        TreeNodeRenderer renderer = new TreeNodeRenderer();
    
        public TreeNodeEditor(JTree tree) {
            this.tree=tree;
        }
    
        @Override
        public boolean isCellEditable(EventObject event) {
    
            boolean editable=false;
    
            if (event instanceof MouseEvent) {
    
                MouseEvent mouseEvent = (MouseEvent) event;
                TreePath path = tree.getPathForLocation(mouseEvent.getX(),mouseEvent.getY());
    
                if (path != null) {
    
                    Object node = path.getLastPathComponent();
    
                    if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
    
                        DefaultMutableTreeNode editedNode = (DefaultMutableTreeNode) node;
                        TreeNodeModel model = (TreeNodeModel) editedNode.getUserObject();
                        editable = model.hasCheckBox;
    
                    }   //if (node)
                }   //if (path)
            }   //if (MouseEvent)
    
            return editable;
    
        }
    
        public Object getCellEditorValue() {
    
            JCheckBox checkbox = renderer.getCheckBoxRenderer();
    
            TreeNodeModel model = new TreeNodeModel(editedModel.getKey(), checkbox.getText(), true, checkbox.isSelected());
            return model;
    
        }
    
        public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
    
            if (((DefaultMutableTreeNode) value).getUserObject() instanceof TreeNodeModel) {
                editedModel = (TreeNodeModel) ((DefaultMutableTreeNode) value).getUserObject();
            }
    
            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;
        }
    
    }
    

    The key was capturing the model in the getTreeCellEditorComponent() method and using its Key in the getCellEditorValue() method.

    Building the tree was easy:

    DefaultMutableTreeNode server = new DefaultMutableTreeNode(new TreeNodeModel(0,"Server 01", false));
    
    DefaultMutableTreeNode userFolder = new DefaultMutableTreeNode(new TreeNodeModel(1, "User Folders", false));
    server.add(userFolder);
    
    DefaultMutableTreeNode user01 =  new DefaultMutableTreeNode(new TreeNodeModel(2, "User 01", true, true));
    userFolder.add(user01);
    
    DefaultMutableTreeNode clientA = new DefaultMutableTreeNode(new TreeNodeModel(3, "Client A", true, true));
    user01.add(clientA);
    
    DefaultMutableTreeNode clientB = new DefaultMutableTreeNode(new TreeNodeModel(4, "Client B", true, true));
    user01.add(clientB);
    
    DefaultMutableTreeNode publicFolder = new DefaultMutableTreeNode(new TreeNodeModel(5, "Public Folders", false));
    server.add(publicFolder);
    
    DefaultMutableTreeNode clientC = new DefaultMutableTreeNode(new TreeNodeModel(6, "Client C", true));
    publicFolder.add(clientC);
            Tree_Nodes.setCellRenderer(new TreeNodeRenderer());
            Tree_Nodes.setCellEditor(new TreeNodeEditor(Tree_Nodes));
    Tree_Nodes.setModel(new DefaultTreeModel(server);
    

    Finally, determining which nodes where checked was a matter of examining the model’s node collection (via a button):

    private Map<Integer, String> checked = new HashMap<Integer, String>();
    
    private void Button_CheckedActionPerformed(java.awt.event.ActionEvent evt) {
    
        DefaultTableModel tableModel = ((DefaultTableModel) Table_Nodes.getModel());
        tableModel.getDataVector().removeAllElements();
        tableModel.fireTableDataChanged();
    
        checked.clear();
    
        DefaultTreeModel treeModel = (DefaultTreeModel) Tree_Nodes.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot();
    
        getChildNodes(root);
    
        for (Iterator it=checked.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry)it.next();
            tableModel.addRow(new Object[] {entry.getKey(), entry.getValue()});
        }
    
        Button_Checked.requestFocus();
    
    
    }
    
    private void getChildNodes(DefaultMutableTreeNode parentNode) {
    
        for (Enumeration e=parentNode.children(); e.hasMoreElements();) {
    
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) e.nextElement();
            TreeNodeModel model = (TreeNodeModel) childNode.getUserObject();
    
            if (model.hasCheckBox && model.isSelected()) {
                checked.put(model.getKey(), model.getValue());
            }
    
            //recurse
            if (childNode.getChildCount()>0) getChildNodes(childNode);
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting the modify this Modx Snippet so that it will accept multiple values
I am essentially attempting to modify this stored procedure. Modified stored procedure: CREATE PROCEDURE
This is already cross-posted at MS Connect: https://connect.microsoft.com/VisualStudio/feedback/details/560451 I am attempting to override the
I'm attempting to modify an inherited project that has a convoluted process of displaying
I'm attempting to modify a MIPS simulator to display the contents of its registers
I've been attempting this for two days, and constantly running into dead ends. I've
This is basically a menu that doesn't refresh the page. I am attempting to
I've been attempting to modify the contents of a custom tree view that inherits
I'm attempting to modify a date field from my database before it gets outputted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.