Universal Robots Forum

Add jtextfields to radiobuttons

hello all,

Sorry for the probably very easy question I’m asking, but I’m new to coding and I can’t figure it out.

I would like to make the following:

Does anyone know if there is an example of a JTextfield in a Jradiobutton?

thanks in advance!

best regards,

Zoë

You can try by setting the label on the button as html.

but, it would be way easier to just create a JradioButton and a separate JTextField object.
so I would just (this is more of a sudo code):

Final= new JPanel();
Final.setLayout(new GridLayout(3,2,1,1));
private JRadioButton button1=new JRadioButton();
button1 =new JRadioButton("button1");
button1.setHorizontalTextPosition(SwingConstants.LEFT);
button1.setSelected(true);

Final.add(button1)
Final.add( new JLabel(" ");
Final.add( new JLabel(" ");
Value = new JTextField();
Value = new JTextField();
Value.setHorizontalAlignment(SwingConstants.LEFT);
final.add(Value);

It looks like you’re asking how to mimic the behavior of the MoveL’s radio buttons regarding Shared Parameters? You are wanting to Enable/Disable a JTextField in response to a radio button press?

If I am understanding your question correctly, then the short answer is no, there is no way to construct a radio button with a JTextField inside it.

However, this is not what is happening here anyway. What’s actually happening is there is a radio button (you may benefit from googling “ButtonGroup” as this is how you can force only one button to be selected) and a JTextField both being placed into another container (likely a “Box.” Google Java Box Layout Manager). These are completely separate components, and are “linked” by simply accessing each other through the “onSelected()” (or similar) callbacks tied to the radio button. Let me show an example.

(There’s a lot of red underlines here because these aren’t real components in my project. Use whatever RadioButton/TextFields you actually have in your code)

I think we looked at how to add click listeners previously in regards to a checkbox? The same principle applies here! We can attach listeners (of which there are many different kinds, this is just the easiest example) that alter the states of other UI elements accordingly.

One thing you may notice is that despite the component being disabled (ie textfield.setEnabled(false) has been run) you are still able to click on the component! This is usually not the desired effect, so we can add an if() condition to the click listener so that the textField only reacts if it is actually enabled.

image

Hopefully this can get you started on the right direction, or at the very least helped you learn what you need to Google haha!

2 Likes

@moliveira @eric.feldmann
thank you both! i’ll get on it, search further on google where necessary and let you know if it worked out!

hello!

The creation and linking of the textfield to the radiobuttons succeeded, but now I run into the following problem that I want to read the information from the textfield only when this button is selected. But how can I extract the selected button from the view and process it in the contribution. i know how to check if a button is checked but not a specific button.
sorry for the (maybe simple) questions, I try to find it on google but I can’t find the answer.

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!

2 Likes

Hello Eric,

Thank you again for your detailed explanation. This helped me to solve it and gave me more insight on how to exchange values between the view and contribution.

Thank you a lot!

Yours sincerely,

Zoë