Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 479
  • Last Modified:

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
0
scurtis_1
Asked:
scurtis_1
  • 5
  • 3
  • 2
  • +1
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now