Universal Robots Forum

How to create a jCheckbox

Hello,

I am a student who wants to write a simple URCap for her final project. I am completely new as far as Java is concerned so I still find the language quite difficult.

I unfortunately can’t find a clear example on how to create a “JCheckbox” in the programNodeView & programNodeContribution on the universal site. Does anyone have a simple example for me?

Thanks in advance for your help.

Greetings Zoe

Below is an example for declaring, initializing, and attaching a listener to a checkbox. This is done in the ProgramNodeView. The checkbox itself can be constructed outside of a function, but configuring it must be done inside a function. I have included a few of the methods that go along with the MouseListener class, such as mouseClicked, mouseReleased, etc. These methods are overriden from the MouseListener class, so you don’t really need to know what’s happening, other than understanding that whatever code you put in (for example) the mouseClicked method will be called every time the checkbox is clicked. You’ll see for that particular method, I am going to pass the state of the checkbox into the function so I can determine later if it was being clicked ON or being clicked OFF.

Note: There are many different kinds of listeners, and some are much more elegant than sending the state of the checkbox in, but I’m choosing this as a simple illustration.

From this point, documentation online becomes very useful for seeing what types of methods you have available. Often times I will simply type something like “myCheckBox.” and see what sort of methods are suggested after my “.”

You’ll also notice here that it’s mad at my “contribution” and that’s because it’s not a real variable. Replace that with whatever you’ve named your contribution and passed to the view. This should be what you’re passing in the buildUI() function.

Now you need to create a method in your ProgramNodeContribution that can perform whatever actions you need based on that user clicking the box.

image

You will also likely need to incorporate some basic get() functions in your contribution for getting the state of the checkbox. This will become essential to be used in the openView() method to ensure that when you switch from one screen to the next, it loads the correct state of the checkbox.

Finally, if you do all this, if you’re like me you’ll load up your simulator and wonder why your checkbox isn’t showing up. This is because you’ve created it, configured it, but haven’t added it to the UI yet! In the “buildUI()” function in your programNodeView you will notice you are provided a JPanel panel. You will need to call something along the lines of “panel.add(myExampleCheckbox)” as this will actually put your component (your checkbox) on the UI panel.

As you dig deeper into Java, you will want to lookup “layout managers” and how to use them. They are pretty in depth and can be very finicky, but will make life easier when laying out your UI if you take the time to understand them.

Hopefully this can help you get started!

1 Like

hello Eric,

What a fine extensive example! I can certainly move forward with this! thank you very much for your efforts

best regards,

Zoë