?
Solved

JCheckbox detect mouse click over programmatic setting?

Posted on 2005-02-28
11
Medium Priority
?
461 Views
Last Modified: 2012-08-13
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
Comment
Question by:scurtis_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13418436
You probably need to create your own subclass and alter the methods
0
 
LVL 37

Accepted Solution

by:
zzynx earned 400 total points
ID: 13418439
I always work with a flag.

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


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

   // Other stuff
}
0
 

Author Comment

by:scurtis_1
ID: 13418442
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 37

Expert Comment

by:zzynx
ID: 13418446
>> I always work with a flag.
...in combination with an ActionListener
0
 

Author Comment

by:scurtis_1
ID: 13418451
zzynx,

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

Thanks
Scott
0
 
LVL 1

Expert Comment

by:anindyabecs
ID: 13418458
Hi Scott,
            Can u please send the code for u'r programme.. So that I can go through it...
                                    Expecting your code -
                                                                        Anindya
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13418464
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
 

Author Comment

by:scurtis_1
ID: 13418499
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13418581
>> I like that. Simple, yet effective! I will give it a try.
I would do that too ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13418781
Thanks for accepting
0
 
LVL 1

Expert Comment

by:anindyabecs
ID: 13418835
Thnaks scrutis,
                 For sending the code..
                                                  - Anindya
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question