Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Shoss

Thread: Why does the check box untick itself?

  1. #1
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Question Why does the check box untick itself?

    I have a program which has a GUI control that I designed. It consists of several check boxes inside a panel, the user can tick one or more of them. There are various methods to get all the selections, untick boxes in code etc. In one place in my software, I need a dialog box to appear when the user ticks a box. So I got my control to raise a property change event whenever one of the check boxes is ticked or unticked. I then catch this in another part of the code, and cause a dialog to appear which asks the user for further options.

    My problem is this: for some strange reason, when the dialog appears, it is causing the check box to be unticked again immediately after it is ticked. I have prepared a simplified version of the problem code, below. If you comment out the call to ticking(), then it works ok (but the dialog doesn't appear obviously). Has anyone got any ideas, please?

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.*;
    import java.beans.*;
    import java.util.HashSet;
    import javax.swing.*;
    
    public class Testing {
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override public void run() {                  
                    Testing testing = new Testing();
                }
            });
        } 
        
        public Testing() {
            JFrame fmMain = new JFrame();
            CheckBoxes cb = new CheckBoxes("one", "two", "three");
            cb.addPropertyChangeListener(new PropertyChangeListener() {     
                @Override public void propertyChange(PropertyChangeEvent pce) {
                    String category = (String) pce.getPropertyName();
                    if(category.equals("ticking")) {             
                        ticking();  //commenting this out means the boxes are ticked ok
                    }
                }
            });
            fmMain.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            fmMain.add(cb);
            fmMain.pack();
            fmMain.setVisible(true);
        }
        
        private void ticking() {
            final JDialog dlg = new JDialog();
            JButton okbutton = new JButton("OK");
            okbutton.addActionListener(new ActionListener() {
                @Override public void actionPerformed(ActionEvent ae) {
                    dlg.dispose();
                }
            });
            dlg.add(okbutton);
            dlg.setModal(true);
            dlg.pack();
            dlg.setVisible(true);  
        }
        
        class CheckBoxes extends JScrollPane {
            public CheckBoxes(Object... arr) {
                super();
                GridLayout gl = new GridLayout();
                gl.setColumns(1);
            
                JPanel panel = new JPanel();
                setViewportView(panel);
                gl.setRows(arr.length);
                panel.setLayout(gl);
            
                ItemListener cl = new ItemListener() {
                    @Override public void itemStateChanged(ItemEvent ie) {
                        JCheckBox cbi = (JCheckBox) ie.getSource();
                        String propertyName = cbi.isSelected() ? "ticking" : "unticking";      
                        System.out.println("Item event: "+propertyName);
                        CheckBoxes.this.firePropertyChange(propertyName, null, cbi.getText());
                    }
                };
            
                HashSet data = new HashSet();
                for(Object o: arr) {
                    if(!data.contains(o)) {
                        JCheckBox candidate = new JCheckBox(o.toString());
                        panel.add(candidate);
                        candidate.addItemListener(cl);
                        data.add(o);
                    }
                }
            }     
        }
    }

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Why does the check box untick itself?

    Not sure why it behaves that way, but this post seems to be exactly what is happening to you. They gave up and switched to using an actionlistener so maybe you can go that route as well.
    pbrockway2 likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Why does the check box untick itself?

    I'm no expert, but you call dlg.setVisible(true) for the modal dialog dlg on the event dispatch thread while responding to the selection event. This ought to be OK as the docs say "It is OK to call this method from the event dispatching thread because the toolkit ensures that other events are not blocked while this method is blocked".

    But after the setVisible() call is made - and before it returns - you get another item state change (the unticking one). This time the itemStateChanged() gets to fully complete. Only much later when the dialog is closed does the original itemStateChanged() method get a chance to finish.

    If you allow the first itemStateChange() to finish before showing the dialog, then everything is OK. Like this:

    Java Code:
    cb.addPropertyChangeListener(new PropertyChangeListener() {    
        @Override public void propertyChange(PropertyChangeEvent pce) {
            String category = (String) pce.getPropertyName();
            System.out.println("category=" + category);
            if(category.equals("ticking")) {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override public void run() {
                        ticking();
                    }
                });
                //ticking();  //commenting this out means the boxes are ticked ok
            }
        }
    });
    It would be nice if someone could explain the exact sequence of events (and where that might be documented). (Possibly: the alternative might be that this is just not the way to do things;)

    [Edit] Since you are treating the checkboxes as buttons whose "clicking" constitutes an event, Shoss' idea of using an action listener is a good one. Ie attach an action listener not an item listener to the check boxes.
    Last edited by pbrockway2; 11-05-2011 at 04:08 AM.

  4. #4
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default Re: Why does the check box untick itself?

    The problem is solved as per my post 10-19-2011 12:13 PM, so I am reluctant to make any changes to the code now as it works.

    If anyone has a good explanation of why it actually happens, I'd be interested to hear it... is it mentioned in the documentation anywhere, I wonder?

Similar Threads

  1. check this out
    By RajeshReddy in forum New To Java
    Replies: 10
    Last Post: 07-25-2011, 01:28 PM
  2. Can someone check this over?
    By Phyxed in forum New To Java
    Replies: 2
    Last Post: 03-29-2011, 10:07 AM
  3. Don't know what to check for
    By Chasingxsuns in forum New To Java
    Replies: 8
    Last Post: 08-26-2009, 06:59 AM
  4. pls check it
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-24-2009, 03:35 PM
  5. Check box tag
    By elizaabru in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-26-2008, 03:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •