MouseListener with CheckBoxTree

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

MouseListener with CheckBoxTree

Postby Daan » Tue Nov 29, 2011 5:51 am

Hey all,

I am using a CheckBoxTree and want to detect clicks on the items in it. To this end, I created a MouseListener which I added to my CheckBoxTree.

In my mouselistener I do the following to get the TreePath that is clicked:
Code: Select all
public void mousePressed( MouseEvent e )
    {
        if ( e.getComponent() instanceof CheckBoxTree )
        {
            CheckBoxTree tree = (CheckBoxTree)e.getComponent();
            TreePath path = tree.getPathForLocation( e.getX(),  e.getY() );
        }
    }


Now, I only want to do something when the label of the path is clicked, not the checkbox. I cannot figure out how to check this. I tried calling getComponentAt(x,y) several times on the tree (as each call only goes 1 level deep), but this keeps returning the CheckBoxTree.

Any ideas?
Daan
 
Posts: 3
Joined: Tue Nov 29, 2011 5:40 am

Re: MouseListener with CheckBoxTree

Postby Daan » Tue Nov 29, 2011 8:21 am

Ok I managed to work around it in the following manner:
ask the CheckBoxTree whether the path is selected, if it isn't, do nothing.
If the path is selected, I can call some other (application specific) functions to determine whether some function (the one that needs to be called when the user clicks on the label and not the checkbox) needs to be called.

But this is not very elegant, to say the least. I would still like to know if there is a way to use only the MouseEvent and the CheckBoxTree to determine whether the mouse clicked occurred on a checkbox or a label.
Daan
 
Posts: 3
Joined: Tue Nov 29, 2011 5:40 am

Re: MouseListener with CheckBoxTree

Postby JIDE Support » Tue Nov 29, 2011 9:16 am

Please give the following code a try.
Code: Select all
        int _hotspot = new JCheckBox().getPreferredSize().width;
        protected boolean clicksInCheckBox(MouseEvent e, TreePath path) {
            if (!_tree.isCheckBoxVisible(path)) {
                return false;
            }
            else {
                Rectangle bounds = _tree.getPathBounds(path);
                if (_tree.getComponentOrientation().isLeftToRight()) {
                    return e.getX() < bounds.x + _hotspot;
                }
                else {
                    return e.getX() > bounds.x + bounds.width - _hotspot;
                }
            }
        }

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 36468
Joined: Sun Sep 14, 2003 10:49 am

Re: MouseListener with CheckBoxTree

Postby Daan » Wed Nov 30, 2011 2:21 am

Works like a charm! Thanks!
Daan
 
Posts: 3
Joined: Tue Nov 29, 2011 5:40 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 4 guests

cron