No need to apologize for “simple” questions! Nothing is simple when you first start out.
Since I’m not sure what your application is exactly, lets say you want to print the contents of the JTextfield but only if the first radio button is selected. Lets say we also put this code into our openView() method, so that it will run every time we open the screen. (I do this a lot as a cheap way of triggering code to run that I want to test)
Lets also go ahead and declare another model string, maybe called “RADIO_BUTTON_NUMBER.”
So at the top of the Contribution we can put:
private static final String RADIO_BUTTON_NUMBER = "RADIO_BUTTON_NUMBER";
This will be what we reference when needing to determine which button is selected. Now in the view, we should have a listener attached to each button. It should call some function in the contribution, such as “onRadioButton1Clicked()”. You could make a second method in the other radio button’s listener called “onRadioButton2Clicked()”, but it’s a good idea to reuse as much code as possible. So instead, we can make a more generic function called “onRadioButtonClicked()” and then pass it a parameter. That parameter is then whatever you want to ID your button as. If I clicked the first radio button, maybe I call “onRadioButtonClicked(1)” and if I click the second radio button, I can call “onRadioButtonClicked(2)”. In this way, I am using the same function for each case. This makes it expandable, as you could have any number of radio buttons, and only ever use this 1 function.
Now in the contirbution, I must define this “onRadioButtonClicked” function, and it must take, in this case, an integer. So lets do:
public void onRadioButtonClicked(int buttonNumber) {
model.set(RADIO_BUTTON_NUMBER, buttonNumber);
}
Now we can finally make some conditional decisions based on the selected button. In our openView() we can say:
if(model.get(RADIO_BUTTON_NUMBER, 0) == 1) {
System.out.println("The first radio button is selected so I will print the textfield contents" + [whatever_your_textfield_is.getText()])
} else if(model.get(RADIO_BUTTON_NUMBER, 0) == 2 {
System.out.println("The second radio button is clicked, so I will NOT print the textfield contents")
}
I would also advise, for cleaner code, to write some “getter” methods for accessing model data, as writing model.get() everywhere looks gross. So for example:
private getRadioButtonSelected() {
return model.get(RADIO_BUTTON_NUMBER, 0);
}
Now the previous code can look like:
if(getRadioButtonSelected()) == 1) {
System.out.println("The first radio button is selected so I will print the textfield contents" + [whatever_your_textfield_is.getText()])
} else if(getRadioButtonSelected()) == 2 {
System.out.println("The second radio button is clicked, so I will NOT print the textfield contents")
} else if(getRadioButtonSelected() == 0) {
System.out.println("Key not found in model. Printing the default value" + getRadioButtonSelected())
}
I also went ahead and added a 3rd case, which is if the datamodel had not been set. If you ever call a model.get() command and the first string parameter had not been model.set(), it will always return the second parameter, which in this example is 0.
As you continue to program, you will notice that the predefined types offer a lot of useful generic methods, such as being able to tell when something has been clicked, however it is often up to you to create additional information that you deem useful on your own. And this is exactly what we’ve done in this example. While the radio buttons did not offer information regarding which one had been clicked by default, we have now added this functionality on our own. This is where out-of-the-box thinking comes in handy, and is a skill you will develop as you program more, and become aware of which tools/commands you have available.
Hope this helps!