# Link JButtons to DigitalO (Swing)

**URL:** <https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214>\
**Category:** URScript\
**Created:** [May 5, 2020, 2:39pm UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214 "2020-05-05T14:39:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon29969749](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon29969749](https://forum.universal-robots.com/u/anon29969749)\
**Post date:** [May 5, 2020, 2:39pm UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/1 "2020-05-05T14:39:33Z")

</div>

Hello Everyone,

I am a beginner with the URCap Swing Programming and require a help with this. I have to create Jbuttons and Radio Buttons on the Program node display and integrate it to Tool digital IO’s using swing programming method.

I have created the JButtons and Radio buttons included the actionListeners too. Now i am unable to link the DigitalIO using actionListener. I am not able to get the right way to declare the digitalIO or use it inside the ActionListener just to turn ON or OFF. I did refer to [com.ur.urcap.sample.manipulateIO.tar.gz] and used similar comand lines still not able to get it right. Can anybody help me with it or Refer me to a sample program which would help me?

Thanks for the time

---

<div class="post-metadata">

**Author:** ![anon81989367](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anon81989367](https://forum.universal-robots.com/u/anon81989367)\
**Post date:** [May 5, 2020, 2:52pm UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/2 "2020-05-05T14:52:41Z")

</div>

program node view:  
button.addActionListener(new ActionListener(){  
@Ovveride  
public void actionPerformed(ActionEvent e){  
provider.get().yourMethod();}});

Program Node Contrribution:  
public void yourMethod(){  
DigitalIO output=getDigitalIO(tool\_out[0]):  
if(output.getValue()==false){  
output.setValue(true);  
}else{  
output.setValue(false);  
}

this is a little example of how it works generally

}

---

<div class="post-metadata">

**Author:** ![anon29969749](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon29969749](https://forum.universal-robots.com/u/anon29969749)\
**Post date:** [May 5, 2020, 3:18pm UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/4 "2020-05-05T15:18:15Z")

</div>

Firstly thanks a lot for the quick reply. I tried your solution and got these errors  
tool\_out[0] - cannot be resolved to a variable, so i changed to the way i declared the DigitalIO  
private DigitalIO tool\_in1;  
then there is error with getDigitalIO - “The method getDigitalIO(DigitalIO) is undefined for the type JButtonProgramNodeContribution”. So should i create a method DigitalIO? Very sorry if it was a basic question.

Thank you

---

<div class="post-metadata">

**Author:** ![m.birkholz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/m.birkholz/32/7084_2.png) [@m.birkholz](https://forum.universal-robots.com/u/m.birkholz)\
**Post date:** [May 6, 2020, 6:07am UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/5 "2020-05-06T06:07:03Z")

</div>

To get your IO in your contribution:

```
    private final String TOOL_IO_KEY = "tool_in[0]";
    DigitalIO toolIO0;
    Collection<IO> ios = apiProvider.getProgramAPI().getIOModel().getIOs(IOFilterFactory.toolFilter());
    Iterator<IO> it = ios.iterator();
    while (it.hasNext()) {
        IO nextIO = it.next();
        if (nextIO.getName().contains(TOOL_IO_KEY)) {
            toolIO0 = nextIO;
            break;
        }
    }

```

having toolIO0 you can fire a setter and getter to actually fire the IO with your button e.g.

```
public void setToolIO(boolean signal){
    toolIO0.setValue(signal);
}

```

In your view:

```
    ioBtn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            provider.get().setToolIO(true);
        }
    });
```

---

<div class="post-metadata">

**Author:** ![anon81989367](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anon81989367](https://forum.universal-robots.com/u/anon81989367)\
**Post date:** [May 6, 2020, 9:37am UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/6 "2020-05-06T09:37:06Z")

</div>

```
DigitalIO getDigitalIO(String defaultName){
	Collection<DigitalIO> IOcollection = apiProvider.getInstallationAPI().getIOModel().getIOs(DigitalIO.class);
	int IO_count = IOcollection.size();
	if(IO_count > 0){
		Iterator<DigitalIO> IO_itr = IOcollection.iterator();
		while(IO_itr.hasNext()){
			DigitalIO thisIO = IO_itr.next();
			String thisDefaultName = thisIO.getDefaultName();

			if(thisDefaultName.equals(defaultName)){
				return thisIO;
			}
		}
	}
	return null;
}

```

You can use this one

---

<div class="post-metadata">

**Author:** ![anon29969749](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon29969749](https://forum.universal-robots.com/u/anon29969749)\
**Post date:** [May 11, 2020, 9:37am UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/7 "2020-05-11T09:37:48Z")

</div>

Thanks a lot for taking the time to help me 🙂

---

<div class="post-metadata">

**Author:** ![anon29969749](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@anon29969749](https://forum.universal-robots.com/u/anon29969749)\
**Post date:** [May 11, 2020, 9:39am UTC](https://forum.universal-robots.com/t/link-jbuttons-to-digitalo-swing/7214/8 "2020-05-11T09:39:14Z")

</div>

Thanks a lot, the code worked. Had a little things to change. Thanks again for the effort 🙂 🙂
