Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Radio Button Selected

Radio Button Selected

Scheduled Pinned Locked Moved Java
question
12 Posts 4 Posters 44 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MallardsReach
    wrote on last edited by
    #1

    Below is the code for 12 radiobuttons it compiles and runs but when I test out the first 2 buttons nothing happens. I have setActionCommand in the loop for each button so What is it I've not done?

    	ButtonGroup bgTableButtons = new ButtonGroup(); 
    				
    		JRadioButton\[\] rbTable = new JRadioButton\[13\];
    		
    			int y = 20;
    			for (int i = 1; i < rbTable.length; i++)
    				{
    					rbTable\[i\] = new JRadioButton(Integer.toString(i) + " Times");
    					rbTable\[i\].setActionCommand(Integer.toString(i));
    					rbTable\[i\].setBounds(320, y, 80, 20);
    					bgTableButtons.add(rbTable\[i\]);
    					panel.add(rbTable\[i\]);
    					y = y + 20;
    				} //End of for loop
           
    	if(rbTable\[1\].isSelected())
    	{
           userText.setText("Button 1");
        }
        else if(rbTable\[2\].isSelected())
    	{
            userText.setText("Button 2");
        } // End of if
    
    Z L 2 Replies Last reply
    0
    • M MallardsReach

      Below is the code for 12 radiobuttons it compiles and runs but when I test out the first 2 buttons nothing happens. I have setActionCommand in the loop for each button so What is it I've not done?

      	ButtonGroup bgTableButtons = new ButtonGroup(); 
      				
      		JRadioButton\[\] rbTable = new JRadioButton\[13\];
      		
      			int y = 20;
      			for (int i = 1; i < rbTable.length; i++)
      				{
      					rbTable\[i\] = new JRadioButton(Integer.toString(i) + " Times");
      					rbTable\[i\].setActionCommand(Integer.toString(i));
      					rbTable\[i\].setBounds(320, y, 80, 20);
      					bgTableButtons.add(rbTable\[i\]);
      					panel.add(rbTable\[i\]);
      					y = y + 20;
      				} //End of for loop
             
      	if(rbTable\[1\].isSelected())
      	{
             userText.setText("Button 1");
          }
          else if(rbTable\[2\].isSelected())
      	{
              userText.setText("Button 2");
          } // End of if
      
      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      Do you have code for that implements the action command?

      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

      M 1 Reply Last reply
      0
      • Z ZurdoDev

        Do you have code for that implements the action command?

        Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

        M Offline
        M Offline
        MallardsReach
        wrote on last edited by
        #3

        I have the following imports that's all What other code do I need? the examles I've looked just show isSelected and that's all.

        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;

        Z 1 Reply Last reply
        0
        • M MallardsReach

          I have the following imports that's all What other code do I need? the examles I've looked just show isSelected and that's all.

          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;

          Z Offline
          Z Offline
          ZurdoDev
          wrote on last edited by
          #4

          Why are you setting setActionCommand to a number?

          Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

          M 1 Reply Last reply
          0
          • Z ZurdoDev

            Why are you setting setActionCommand to a number?

            Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

            M Offline
            M Offline
            MallardsReach
            wrote on last edited by
            #5

            Because I tried setting it to the name of the radiobutton but that didn't work either.

            rbTable[i].setActionCommand(Integer.toString(i) + " Times");

            Z 1 Reply Last reply
            0
            • M MallardsReach

              Because I tried setting it to the name of the radiobutton but that didn't work either.

              rbTable[i].setActionCommand(Integer.toString(i) + " Times");

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #6

              Do you know what the command is for?

              Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

              1 Reply Last reply
              0
              • M MallardsReach

                Below is the code for 12 radiobuttons it compiles and runs but when I test out the first 2 buttons nothing happens. I have setActionCommand in the loop for each button so What is it I've not done?

                	ButtonGroup bgTableButtons = new ButtonGroup(); 
                				
                		JRadioButton\[\] rbTable = new JRadioButton\[13\];
                		
                			int y = 20;
                			for (int i = 1; i < rbTable.length; i++)
                				{
                					rbTable\[i\] = new JRadioButton(Integer.toString(i) + " Times");
                					rbTable\[i\].setActionCommand(Integer.toString(i));
                					rbTable\[i\].setBounds(320, y, 80, 20);
                					bgTableButtons.add(rbTable\[i\]);
                					panel.add(rbTable\[i\]);
                					y = y + 20;
                				} //End of for loop
                       
                	if(rbTable\[1\].isSelected())
                	{
                       userText.setText("Button 1");
                    }
                    else if(rbTable\[2\].isSelected())
                	{
                        userText.setText("Button 2");
                    } // End of if
                
                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That is not the way to do it I am afraid. Visual components use event handlers which are signalled when the events (e.g. pushing a button) occurs. So you need to use the Button (Java Platform SE 7 ) addActionListener[^] method so your code gets called at the relevant time. I would again strongly suggest you go to The Java™ Tutorials[^] and work through them a few times. Trying to learn a language and framework by posting questions here (or any other forum) will take you much longer to get to where you want.

                M 1 Reply Last reply
                0
                • L Lost User

                  That is not the way to do it I am afraid. Visual components use event handlers which are signalled when the events (e.g. pushing a button) occurs. So you need to use the Button (Java Platform SE 7 ) addActionListener[^] method so your code gets called at the relevant time. I would again strongly suggest you go to The Java™ Tutorials[^] and work through them a few times. Trying to learn a language and framework by posting questions here (or any other forum) will take you much longer to get to where you want.

                  M Offline
                  M Offline
                  MallardsReach
                  wrote on last edited by
                  #8

                  Thanks Richard I have and do look at the Java Tutorials but don't always find them helpful even though they explain the concepts of components. I did eventually figure it out, the code is below for anyone else to look through,

                  		ItemListener itemListener = new ItemListener() {
                  			public void itemStateChanged(ItemEvent itemEvent) {
                  				AbstractButton aButton = (AbstractButton)itemEvent.getSource();
                  					int state = itemEvent.getStateChange();
                  					String name = aButton.getText();
                  				for (int i = 1; i < rbTable.length; i++)
                  				{	
                  					if (state == ItemEvent.SELECTED) {
                  						if (name.equals(rbTable\[i\].getText())) {
                  							userText.setText(rbTable\[i\].getText());
                  						}
                  
                  					}
                  				}	
                  			}  
                  		};
                  
                  L 1 Reply Last reply
                  0
                  • M MallardsReach

                    Thanks Richard I have and do look at the Java Tutorials but don't always find them helpful even though they explain the concepts of components. I did eventually figure it out, the code is below for anyone else to look through,

                    		ItemListener itemListener = new ItemListener() {
                    			public void itemStateChanged(ItemEvent itemEvent) {
                    				AbstractButton aButton = (AbstractButton)itemEvent.getSource();
                    					int state = itemEvent.getStateChange();
                    					String name = aButton.getText();
                    				for (int i = 1; i < rbTable.length; i++)
                    				{	
                    					if (state == ItemEvent.SELECTED) {
                    						if (name.equals(rbTable\[i\].getText())) {
                    							userText.setText(rbTable\[i\].getText());
                    						}
                    
                    					}
                    				}	
                    			}  
                    		};
                    
                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    MallardsReach wrote:

                    don't always find them helpful even though they explain the concepts of components.

                    What about all the sample code that's included? Seriously, you really should persevere with them. They teach things in a structured order and you get all the basic concepts well established before moving on to the more advanced stuff. I used the published version of these years ago when I first started working in Java, and have gone back and repeated the online versions a number of times.

                    M 2 Replies Last reply
                    0
                    • L Lost User

                      MallardsReach wrote:

                      don't always find them helpful even though they explain the concepts of components.

                      What about all the sample code that's included? Seriously, you really should persevere with them. They teach things in a structured order and you get all the basic concepts well established before moving on to the more advanced stuff. I used the published version of these years ago when I first started working in Java, and have gone back and repeated the online versions a number of times.

                      M Offline
                      M Offline
                      MallardsReach
                      wrote on last edited by
                      #10

                      Hi Richard, Can't argue and have no wish to, with the points you make about the sample code, The thing is they are stand alone ie Here's a method, here's how to call it. Here' some radio buttons, here's how to find the one selected. And they work fine the problem is when your project gets more added to it and includes many components and then it's trying to get them to work with each other. I am going to get 'Java: The Complete Reference, Eleventh Edition' as soon as things get back to normal. Until then I have to rely on the tutorials and forums to guide me.

                      1 Reply Last reply
                      0
                      • L Lost User

                        MallardsReach wrote:

                        don't always find them helpful even though they explain the concepts of components.

                        What about all the sample code that's included? Seriously, you really should persevere with them. They teach things in a structured order and you get all the basic concepts well established before moving on to the more advanced stuff. I used the published version of these years ago when I first started working in Java, and have gone back and repeated the online versions a number of times.

                        M Offline
                        M Offline
                        MallardsReach
                        wrote on last edited by
                        #11

                        Talk about making things complicated! Here is the final code, no user defined method was needed, I just had to place the results for pressing a button in the 'itemStateChanged method'

                        		ItemListener itemListener = new ItemListener() {
                        			public void itemStateChanged(ItemEvent itemEvent) {
                        				AbstractButton aButton = (AbstractButton)itemEvent.getSource();
                        					int state = itemEvent.getStateChange();
                        					String name = aButton.getText();
                        				for (int i = 1; i < rbTable.length; i++)
                        				{	
                        					if (state == ItemEvent.SELECTED) {
                        						if (name.equals(rbTable\[i\].getText())) {
                        							displayTable.setText(""); //Clear the displayTable 
                        							int firstNum, answerNum;
                        							for (firstNum = 1; firstNum <=12; firstNum ++)
                        							{
                        								answerNum = firstNum \* i;
                        								displayTable.append(" " + firstNum + " x " + i + " = " + answerNum +"\\n");
                        							}
                        			
                        						}
                        					}
                        				}
                        			}  
                        		};
                        
                        B 1 Reply Last reply
                        0
                        • M MallardsReach

                          Talk about making things complicated! Here is the final code, no user defined method was needed, I just had to place the results for pressing a button in the 'itemStateChanged method'

                          		ItemListener itemListener = new ItemListener() {
                          			public void itemStateChanged(ItemEvent itemEvent) {
                          				AbstractButton aButton = (AbstractButton)itemEvent.getSource();
                          					int state = itemEvent.getStateChange();
                          					String name = aButton.getText();
                          				for (int i = 1; i < rbTable.length; i++)
                          				{	
                          					if (state == ItemEvent.SELECTED) {
                          						if (name.equals(rbTable\[i\].getText())) {
                          							displayTable.setText(""); //Clear the displayTable 
                          							int firstNum, answerNum;
                          							for (firstNum = 1; firstNum <=12; firstNum ++)
                          							{
                          								answerNum = firstNum \* i;
                          								displayTable.append(" " + firstNum + " x " + i + " = " + answerNum +"\\n");
                          							}
                          			
                          						}
                          					}
                          				}
                          			}  
                          		};
                          
                          B Offline
                          B Offline
                          Brenda Jasprizza
                          wrote on last edited by
                          #12

                          thanks for your answer

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups