I NEED TO KNOW HOW TO GET THE CHOSEN RADIO BUTTON.MAYBE THE COUNTERPART OF getText FOR RADIO BUTTON... I HAVE ONLY MALE AND FEMALE.

Recommended Answers

All 11 Replies

getSelection() method for the ButtonGroup that they are both in.

getSelection() method for the ButtonGroup that they are both in.

it still doesen't work. here is my code ca you help

else if(done.getActionCommand().equals("Submit"))
			{
//CONDITIONS


			try{
				v1 = jUser.getText();
				v2 = jPass.getText();
				v3 = jConfirm.getText();
				v4 = jName.getText();
				v5 = jLast.getText();
				v6 =(String) combo.getSelectedItem();
				v7 = (String) combo1.getSelectedItem();
				v8 = (String) combo2.getSelectedItem();
				v9 = jAdd.getText();
				v10 = jEmail.getText();
				v11 = jContact.getText();
				v12=bg.getSelection();
			
                                if(!v2.equals(v3))
			{

			JOptionPane.showMessageDialog(null,"Your Password Did Not Match",
			"System Log", JOptionPane.ERROR_MESSAGE);

			}
			else{

				if( (v1.equals("")) || (v2.equals("")) || (v3.equals("")) || (v4.equals("")) || (v9.equals(""))
						|| (v10.equals("")) || (v11.equals("")) )
				{
				JOptionPane.showMessageDialog(null,"Some Text Fields Are Empty",
				 "System Log", JOptionPane.ERROR_MESSAGE);
				}
//PASSWORD CONFIRMATION
                                if(fem.isSelected()){
                                    bg.getSelection();
                                    System.out.print("Female");
                                    
                                }
                                  if(m.isSelected()){
                                 System.out.print("Male");
  
                                  }
				else
				{


				File f = new File("D:/Users/" + v1 + ".txt");
				FileWriter fr = new FileWriter(f);

				fr.write(v1+ "\r\n");
				fr.write(v2 + "\r\n");
				fr.write(v4+ "\r\n");
				fr.write(v5+ "\r\n");
				fr.write(v6+ "/");
				fr.write(v7+ "/");
				fr.write(v8+ "\r\n");
				fr.write(v9+ "\r\n");
				fr.write(v10+ "\r\n");
				fr.write(v11+ "\r\n");
				fr.write(v12+ "\r\n");
			        fr.write(v13);
                                fr.close();

				JOptionPane.showMessageDialog(null,"Your Account Has Been Successfully Created",
				 "System Log", JOptionPane.INFORMATION_MESSAGE);

				LogIn l = new LogIn();
				l.launch();

                 		f2.dispose();

				}//2nd else
			}//1st else


			}//try




			catch(IOException ioe)
				{
					System.out.print("failed");
				}//catch
			}//else if
		}//listener

}//regForm

In what way doesn't it work? Please be specific. Does it print Male or Female?
The else on line 45 goes with the if on line 41, which seems unlikely to be what you intended.

i dont know what to do. haha. what i wanted is that when the female button is selected .it will be save and female is written like what i did in getting the text for names and etc.
the file writer thing. hope you get me hihi.Thanks.

the way i got round finding out which radio button is selected is to create a method that returns which radio button is selected

private String RadioSelected()
{
if(radio1.isSelected()
{
return radio1.getText();
}
if(radio2.isSelected()
{
return radio2.getText();
}
return null;
}

this is only a good idea if you only have 2 or 3 radio buttons would not advice this if using lots of radio buttons

also the value it returns makes printing the value really simple.

System.out.print(RadioSelected());

@ajst
That's what getSelection() method for ButtonGroup does, for any number of buttons (apart from getting the text).
Since the thing you are most likely to do next is to perform different actions or set different data values based on which button is selected, the text doesn't really do much for you. You can't even do a switch on a String! Remember also that Java apps are often translated into different languages, so the text in a Swing component is not reliable for anything. The button itself is better for these purposes.

for the program i used that code in i needed the text so found that to be the easyiest way.

so ive added a radio button group and added the radio buttons to it. now i with the below code i can find out which radio button is selected.

RadioButtonGroup.getSelection().toString()

but my method takes a string which is basicly the name of the radio button to perform the function.

so using a button group how would i do this?

p.s sorry for hijacking this thread.

Yes, if you really want the text that is displayed to the user, then you have to faff about a bit to get the radio button from the ButtonModel so you can getText().
My point was that in a real life app the text displayed to the user is not a good thing to be coding around - never forget localisation. In real life you typically either want the ActionCommand from the ButtonModel or you just want to test which of the possible Buttons this refers to.

so for example if a radio button was used for selecting what user you wished to login with.

how would you then pass that information to the method that takes the user name.
the user name is allready the text of the radio button.

so would it be a better coded function if it worked from what button was selected rather then the way i'm currently doing it?

We're in danger of over-hijacking this thread, so think we should keep this short...
For a real-life implementation of your eg I would architect it as follows:
There would be a User class - name is an attribute, but also password, privileges etc
I would add the User instance for each radio button as a client property of the button (see putClientProperty/getClientProperty)
I would respond to the button selection events to maintain the currently selected User because there may well be other things that become available/unavailable depending on that User's privileges.

To be fair, I am coming at this from a real implementation direction, where the initial requirements are always complex, and you're going to have to update it all the time as the requirements change and grow. For that reason I'm happy to take the up-front hit of coding it in the most extendible way I can reasonably think of.
This is typically not the quickest way to complete a college assignment. ;-)

well thank you for that input. i didn't think of doing the user like that and ill stop asking questions so we dont hijack this thread anymore.
i might have an attempt of recoding the project to a similiar way you said.

This is not a college project its a application i've made to make my life at work easyier and looking to give it to other collegues and my boss. so trying to make it nice tidy code incase they look ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.