Link to home
Start Free TrialLog in
Avatar of scurtis_1
scurtis_1

asked on

JCheckbox detect mouse click over programmatic setting?

Hi,

I have a JCheckbox and I want to perform an action only when the user clicks it and not when a call to setSelected(boolean) is made within my code. I have added an ItemListener to the JCheckbox but unfortunately this picks up any change event including programmatic setting using checkBox.setSelected(boolean). Is it possible to detect the difference between a call to setSelected(boolean) and a user clicking the checkbox? Do I need a different type of listener?

Thanks
Scott
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You probably need to create your own subclass and alter the methods
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scurtis_1
scurtis_1

ASKER

Wow CEHJ! That was quick! :-)

Any ideas what the altered code might look like? How would I detect the mouse click over the method call?

Thanks
Scott
>> I always work with a flag.
...in combination with an ActionListener
zzynx,

I like that. Simple, yet effective! I will give it a try.

Thanks
Scott
Hi Scott,
            Can u please send the code for u'r programme.. So that I can go through it...
                                    Expecting your code -
                                                                        Anindya
So:

public class YourPanelOrWhatever extends JPanel implements ActionListener {

     private JCheckBox theCheckBox;
     private boolean skipChanges = false;

     public YourPanelOrWhatever() {
         theCheckBox.addActionListener(this);
     }


     private void setCheckBoxSelected(boolean selected) {  // <<<<< and use this instead of theCheckbox.setSelected(...)
         skipChanges = true;
         theCheckBox.setSelected(selected);
         skipChanges = false;
     }

     public void actionPerformed(ActionEvent evt) {
           if (skipChanges) return;

           // Other stuff
    }
}
Anindya,

This is basically the gist of it.

*****************************************

JCheckBox useDefaultChkBox = new JCheckBox();

useDefaultChkBox.addItemListener(new ItemStateChangedListener());

class ItemStateChangedListener
            implements ItemListener
{
    public void itemStateChanged(ItemEvent e)
    {

        Object source = e.getItemSelectable();

        if (source == useDefaultChkBox)
        {
            if (e.getStateChange() == ItemEvent.DESELECTED)
            {
                //do stuff
            }
            else
            {
                //do other stuff
            }
        }
    }
}

*****************************************

I only want to handle the ItemStateChanged event when the user physically clicks the check box.

Thanks
Scott
>> I like that. Simple, yet effective! I will give it a try.
I would do that too ;°)
Thanks for accepting
Thnaks scrutis,
                 For sending the code..
                                                  - Anindya