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(boole an). 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
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(boole
Thanks
Scott
You probably need to create your own subclass and alter the methods
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
...in combination with an ActionListener
ASKER
zzynx,
I like that. Simple, yet effective! I will give it a try.
Thanks
Scott
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
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.addActionListe ner(this);
}
private void setCheckBoxSelected(boolea n selected) { // <<<<< and use this instead of theCheckbox.setSelected(.. .)
skipChanges = true;
theCheckBox.setSelected(se lected);
skipChanges = false;
}
public void actionPerformed(ActionEven t evt) {
if (skipChanges) return;
// Other stuff
}
}
public class YourPanelOrWhatever extends JPanel implements ActionListener {
private JCheckBox theCheckBox;
private boolean skipChanges = false;
public YourPanelOrWhatever() {
theCheckBox.addActionListe
}
private void setCheckBoxSelected(boolea
skipChanges = true;
theCheckBox.setSelected(se
skipChanges = false;
}
public void actionPerformed(ActionEven
if (skipChanges) return;
// Other stuff
}
}
ASKER
Anindya,
This is basically the gist of it.
************************** ********** *****
JCheckBox useDefaultChkBox = new JCheckBox();
useDefaultChkBox.addItemLi stener(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
This is basically the gist of it.
**************************
JCheckBox useDefaultChkBox = new JCheckBox();
useDefaultChkBox.addItemLi
class ItemStateChangedListener
implements ItemListener
{
public void itemStateChanged(ItemEvent
{
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 ;°)
I would do that too ;°)
Thanks for accepting
Thnaks scrutis,
For sending the code..
- Anindya
For sending the code..
- Anindya