Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By sehudson

Thread: Two checkboxes not working simultaneously

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Two checkboxes not working simultaneously

    Hi ,

    I am trying to the display the result of the selected Checkboxes on a Label; but having some sort of problem:-

    Please have a look at the code
    Java Code:
    /*<applet code="CheckBoxApplet.class" width="320" height="250"></applet>*/
    
    public class CheckBoxApplet extends java.applet.Applet implements java.awt.event.ActionListener
    {
    	java.awt.Label l1,l2,l3;
    	java.awt.Checkbox ch1,ch2;
    	java.awt.Button b1;
    	public CheckBoxApplet()
    	{
    		setLayout(null);
    		l1 = new java.awt.Label("AntiSpam");
    		l2 = new java.awt.Label("AntiVirus");
    		l3 = new java.awt.Label();
    		b1 = new java.awt.Button("check");
    		ch1 = new java.awt.Checkbox();
    		ch2 = new java.awt.Checkbox();
    		add(ch1);
    		ch1.setBounds(10, 30, 20, 20);
    		add(l1);
    		l1.setBounds(28, 30, 100, 20);
    		add(ch2);
    		ch2.setBounds(10, 60, 20, 20);
    		add(l2);
    		l2.setBounds(28, 60, 100, 20);
    		add(b1);
    		b1.setBounds(10, 90, 45, 30);
    		add(l3);
    		l3.setBounds(10, 140, 100, 20);
    		b1.addActionListener(this);
    		}
    	
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		if(b.getLabel().equals("check"))
    		{
    			if(ch1.getState() == true)
    			{
    				l3.setText(l1.getText());
    			}
    			else if(ch2.getState() == true)
    			{
    				l3.setText(l2.getText());
    			}
    			
    			else if((ch1.getState()&& ch2.getState()) == true)
    			{
    				/*String s = l1.getText() + l2.getText();
    				System.out.println(s);*/
    				l3.setText((l2.getText()+ l1.getText() ));
    			}
    			else
    			{
    				l3.setText(null);
    			}
    			
    		}	
    	}
    }
    Also please find attached is the output I am getting:-
    Two checkboxes not working simultaneously-1.png
    Last edited by ankiit; 04-04-2012 at 08:49 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Two checkboxes not working simultaneously

    having some sort of problem
    Can you explain what the problem is?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    Hi ,

    The problem is if i select both of the Check boxes then the output is the AntiSpam which is as shown in the output; i want to display all the selected text boxes.(ie AntiSpam Antivirus) if both of the boxes are selected.
    Last edited by ankiit; 04-04-2012 at 09:07 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Two checkboxes not working simultaneously

    Your chain of if/else if statements controls what is shown. Check if they are doing what you want done.

    What do you mean by: shown in the output? Is it printed or how is it shown?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    I am using a label to display the selected boxes under my check button; the line below the check button is the output.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Two checkboxes not working simultaneously

    I think your problem is the way your if/else blocks are ordered. For example, if you have both of them checked, then your first if block will be entered:

    Java Code:
    if(ch1.getState() == true)
                {
                    l3.setText(l1.getText());
                }
    And at that point, none of your other if else statements will be evaluated. So think about that, and re-think your if/else logic.
    ankiit likes this.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Two checkboxes not working simultaneously

    what about the first part of my last post?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    yes about the if/else; i am trying to find a way out.

  9. #9
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    I have tried this but this is also not working:-

    Java Code:
    /*<applet code="CheckBoxApplet.class" width="320" height="250"></applet>*/
    
    public class CheckBoxApplet extends java.applet.Applet implements java.awt.event.ActionListener
    {
    	java.awt.Label l1,l2,l3;
    	java.awt.Checkbox ch1,ch2;
    	java.awt.Button b1;
    	public CheckBoxApplet()
    	{
    		setLayout(null);
    		l1 = new java.awt.Label("AntiSpam");
    		l2 = new java.awt.Label("AntiVirus");
    		l3 = new java.awt.Label();
    		b1 = new java.awt.Button("check");
    		ch1 = new java.awt.Checkbox();
    		ch2 = new java.awt.Checkbox();
    		add(ch1);
    		ch1.setBounds(10, 30, 20, 20);
    		add(l1);
    		l1.setBounds(28, 30, 100, 20);
    		add(ch2);
    		ch2.setBounds(10, 60, 20, 20);
    		add(l2);
    		l2.setBounds(28, 60, 100, 20);
    		add(b1);
    		b1.setBounds(10, 90, 45, 30);
    		add(l3);
    		l3.setBounds(10, 140, 100, 20);
    		b1.addActionListener(this);
    	}
    	
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		if(b.getLabel().equals("check"))
    		{
    			if((ch1.getState() || ch2.getState()) == true)
    			{
    				boolean b1 = ch1.getState();
    				boolean b2 = ch2.getState();
    				
    				if(b2 == true)
    				{
    					l3.setText(l2.getText());
    				}
    			
    				else
    				{
    					l3.setText(l1.getText());
    				}
    			}
    			
    			
    			else if((ch1.getState() & ch2.getState()) == false)
    			{
    				/*String s = l1.getText() + l2.getText();
    				System.out.println(s);*/
    				//l3.setText((l2.getText()+ l1.getText() ));
    				l3.setText(null);
    			}
    			else
    			{
    				l3.setText(l1.getText() + " " + l2.getText());
    			}
    			
    		}
    	}
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Two checkboxes not working simultaneously

    Look at the logic and see what it is going to do when executed. Play computer with it, "execute" it statement by statement and see what it does.

    Or try writing down in pseudo code what you want the code to do.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    Thanks for helping, the code works now:-

    Java Code:
    /*<applet code="CheckBoxApplet.class" width="320" height="250"></applet>*/
    
    public class CheckBoxApplet extends java.applet.Applet implements java.awt.event.ActionListener
    {
    	java.awt.Label l1,l2,l3;
    	java.awt.Checkbox ch1,ch2;
    	java.awt.Button b1;
    	public CheckBoxApplet()
    	{
    		setLayout(null);
    		l1 = new java.awt.Label("AntiSpam");
    		l2 = new java.awt.Label("AntiVirus");
    		l3 = new java.awt.Label();
    		b1 = new java.awt.Button("check");
    		ch1 = new java.awt.Checkbox();
    		ch2 = new java.awt.Checkbox();
    		add(ch1);
    		ch1.setBounds(10, 30, 20, 20);
    		add(l1);
    		l1.setBounds(28, 30, 100, 20);
    		add(ch2);
    		ch2.setBounds(10, 60, 20, 20);
    		add(l2);
    		l2.setBounds(28, 60, 100, 20);
    		add(b1);
    		b1.setBounds(10, 90, 45, 30);
    		add(l3);
    		l3.setBounds(10, 140, 150, 20);
    		b1.addActionListener(this);
    	}
    	
    	public void actionPerformed(java.awt.event.ActionEvent e)
    	{
    		java.awt.Button b = (java.awt.Button) e.getSource();
    		if(b.getLabel().equals("check"))
    		{
    			if(ch1.getState() == true)
    			{
    
    			if(ch2.getState() == true)
    			{
    
    			l3.setText (l1.getText() + " " + l2.getText());
    			}
    
    			else
    			{
    			l3.setText(l1.getText());
    			}
    			}
    
    			else if(ch2.getState() == true)
    			{
    			l3.setText(l2.getText());
    			}
    
    			else
    			{
    			l3.setText(null);
    			}
    		}
    	}
    }
    Please find attached is the output:-

    Two checkboxes not working simultaneously-4.pngTwo checkboxes not working simultaneously-1.pngTwo checkboxes not working simultaneously-2.pngTwo checkboxes not working simultaneously-3.png

  12. #12
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Two checkboxes not working simultaneously

    hi sehudson,

    thanks i found the answer. :)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Two checkboxes not working simultaneously

    The formatting of the nested code makes it hard to see the logic. There should NOT be a } directly beneath another }.
    They should be indented 3-4 spaces to show the level of the logic.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Two threads doesn't work simultaneously
    By przenica in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-16-2011, 02:39 PM
  2. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  3. Executing two threads simultaneously.
    By Onra in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 08:03 PM
  4. Why my threads don't run simultaneously?
    By Gilvan Justino in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 01:43 AM
  5. Replies: 1
    Last Post: 12-21-2008, 09:06 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
  •