Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: GUI question: Why can't I uncheck a checkbox?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default GUI question: Why can't I uncheck a checkbox?

    Java Code:
    package myPackage;
    
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class LottoMadness extends JFrame {
    
    	// set up row1
    	JPanel			row1			= new JPanel();
    	ButtonGroup		option			= new ButtonGroup();
    	JCheckBox		quickpick		= new JCheckBox("Quick Pick");
    	JCheckBox		personal		= new JCheckBox("Personal");
    
    	// set layout
    	GridLayout		layout			= new GridLayout(5, 1, 10, 10);
    	FlowLayout		layout1			= new FlowLayout(FlowLayout.CENTER, 10, 10);
    
    	// no arg constructor of LottoMadness
    	public LottoMadness() {
    		super("Lotto Madness");
    		// set size of the JFrame
    		this.setSize(550, 400);
    		// set frame resize not allowed
    		this.setResizable(false);
    		// set default close opeartion
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		// set GridLayout
    		this.setLayout(layout);
    		// add buttons to the buttongroup
    		this.option.add(quickpick);
    		this.option.add(personal);
    		// set Layout for JPanel row1
    		this.row1.setLayout(layout1);
    		// add buttons to JPanel row1
    		row1.add(quickpick);
    		row1.add(personal);
    		// add Jpanel row1 to JFrame
    		this.add(row1);
    		this.setVisible(true);
    	}// end constructor
    
    	public static void main(String[] argumnets) {
    		LottoMadness frame = new LottoMadness();
    	}//end method main()
    
    }// end class
    My question is:

    I see the checkboxes: quickpick and personal.
    But if I check any of them, I can not uncheck again. It seems like one of them has to be in checked state.

    Can anyone help why ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: GUI question: Why can't I uncheck a checkbox?

    It's because you've added them to a ButtonGroup. If you need to clear them, you can do this but perhaps not from the JCheckbox itself. For instance if you gave your gui a "clear" button, you could clear the ButtonGroup itself via its clearSelection() method.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: GUI question: Why can't I uncheck a checkbox?

    Thank you.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: GUI question: Why can't I uncheck a checkbox?

    Normal GUI design is to only use a ButtonGroup for JRadioButtons.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: GUI question: Why can't I uncheck a checkbox?

    Thanks for the tip.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: GUI question: Why can't I uncheck a checkbox?

    Quote Originally Posted by Fubarable View Post
    It's because you've added them to a ButtonGroup. If you need to clear them, you can do this but perhaps not from the JCheckbox itself. For instance if you gave your gui a "clear" button, you could clear the ButtonGroup itself via its clearSelection() method.
    Or extend ButtonGroup to provide the desired functionality via a trivial override:
    Java Code:
             @Override
             public void setSelected(ButtonModel m, boolean b) {
                if (b && m != null && m != getSelection()) {
                   super.setSelected(m, b);
                } else if (!b && m == getSelection()) {
                   clearSelection();
                }
             }
    db

    deselect elements belonging to the same ButtonGroup (Swing / AWT / SWT / JFace forum at JavaRanch)
    Last edited by DarrylBurke; 12-18-2011 at 08:16 PM.
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: GUI question: Why can't I uncheck a checkbox?

    Thank you.

Similar Threads

  1. Replies: 3
    Last Post: 07-27-2010, 09:52 AM
  2. checkbox question
    By HellRider in forum AWT / Swing
    Replies: 1
    Last Post: 02-09-2010, 03:35 PM
  3. CheckBox
    By boss in forum NetBeans
    Replies: 3
    Last Post: 01-17-2010, 04:51 AM
  4. Jtree with checkbox
    By msankar.ravi in forum AWT / Swing
    Replies: 1
    Last Post: 07-09-2009, 11:25 PM
  5. checkbox
    By Alan in forum AWT / Swing
    Replies: 3
    Last Post: 05-18-2007, 10:10 AM

Posting Permissions

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