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
scurtis_1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You probably need to create your own subclass and alter the methods
0
zzynxSoftware engineerCommented:
I always work with a flag.

skipChanges = true;
yourCheckBox.setSelected(....)
skipChanges = false;


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

   // Other stuff
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scurtis_1Author Commented:
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

zzynxSoftware engineerCommented:
>> I always work with a flag.
...in combination with an ActionListener
0
scurtis_1Author Commented:
zzynx,

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

Thanks
Scott
0
anindyabecsCommented:
Hi Scott,
            Can u please send the code for u'r programme.. So that I can go through it...
                                    Expecting your code -
                                                                        Anindya
0
zzynxSoftware engineerCommented:
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
    }
}
0
scurtis_1Author Commented:
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
0
zzynxSoftware engineerCommented:
>> I like that. Simple, yet effective! I will give it a try.
I would do that too ;°)
0
zzynxSoftware engineerCommented:
Thanks for accepting
0
anindyabecsCommented:
Thnaks scrutis,
                 For sending the code..
                                                  - Anindya
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.