# JOptionPane keyboard

**URL:** <https://forum.universal-robots.com/t/joptionpane-keyboard/6783>\
**Category:** Java\
**Created:** [March 25, 2020, 2:18pm UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783 "2020-03-25T14:18:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon89006500](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@anon89006500](https://forum.universal-robots.com/u/anon89006500)\
**Post date:** [March 25, 2020, 2:18pm UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/1 "2020-03-25T14:18:59Z")

</div>

Hi,

I am showing a JOptionPane with an textbox like this:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/e/e709e64277e63b09d1365818141e68837dde2931.png)

This is the code:

> ```
> String s = (String)JOptionPane.showInputDialog(
> frame,
> "Complete the sentence:\n"
> + "\"Green eggs and...\"",
> "Customized Dialog",
> JOptionPane.PLAIN_MESSAGE,
> icon,
> null,
> "ham");
> 
> ```

Do you know how to call the keyboard factory to write in the textbox?

---

<div class="post-metadata">

**Author:** ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)\
**Post date:** [March 26, 2020, 4:13am UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/2 "2020-03-26T04:13:51Z")

</div>

It needs to set eventhandler which call keyboard factory.  
Please try the following.  
I tested on real robot (UR3 SW3.12.0).

```
	private void onClick_btnDialog(InputEvent event) {
	if (event.getEventType() == EventType.ON_PRESSED) {

		KeyboardInputFactory keyboardfactory;
		final KeyboardTextInput keyboard;

		keyboardfactory = api.getInstallationAPIProvider().getUserInterfaceAPI().getUserInteraction()
				.getKeyboardInputFactory();
		keyboard = keyboardfactory.createStringKeyboardInput();

		JPanel panel = new JPanel();
		JTextField textfield = new JTextField(60);

		textfield.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {

				final JTextField text = (JTextField) e.getSource();

				keyboard.show(text, new KeyboardInputCallback<String>() {
					@Override
					public void onOk(String value) {
						text.setText(String.valueOf(value));
					}
				});
			}

		});

		panel.add(textfield);
		JOptionPane optionPane = new JOptionPane(panel);
		optionPane.setBorder(new LineBorder(Color.BLACK, 2));
		JDialog dialog = optionPane.createDialog(null, "TEST");
		dialog.setModal(false);

		dialog.setVisible(true);
	}

}

```

 ![dialog](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/6/6a4a168f5d7a8f4df2022cfde79abe51dfc78758.jpeg)

---

<div class="post-metadata">

**Author:** ![anon89006500](https://avatars.discourse-cdn.com/v4/letter/a/3d9bf3/32.png) [@anon89006500](https://forum.universal-robots.com/u/anon89006500)\
**Post date:** [March 26, 2020, 4:19pm UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/3 "2020-03-26T16:19:10Z")

</div>

OOh Thanks you very much!!

---

<div class="post-metadata">

**Author:** ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)\
**Post date:** [March 27, 2020, 6:11am UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/4 "2020-03-27T06:11:20Z")

</div>

**Important point:**

If dialog is shown, please set modal “false”.

```
dialog.setModal(false);

```

If modal is “true”, you can show keyboard but cannot input.

Best regards.

---

<div class="post-metadata">

**Author:** ![anon47255888](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@anon47255888](https://forum.universal-robots.com/u/anon47255888)\
**Post date:** [April 28, 2021, 11:52am UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/6 "2021-04-28T11:52:14Z")

</div>

Hello @fujikit,

Firstly let me say thank you for you sample.

I am certain that is working like you have specified but I have a different use case.

I have a Dialog window already with a text field and I want just to add the possibility to write in that text field.  
When I copy from you sample just the parts which I need I have an error on the line below:

keyboardfactory = api.getInstallationAPIProvider().getUserInterfaceAPI().getUserInteraction()  
.getKeyboardInputFactory();

api is signaled as wrong and I do not understand why.  
I have imported the Installation Api Provider and still no use.  
Below my code which is causing this issue:

```
KeyboardInputFactory keyboardfactory;
	final KeyboardTextInput keyboard;

	keyboardfactory = api.getInstallationAPIProvider().getUserInterfaceAPI().getUserInteraction()
			.getKeyboardInputFactory();
	keyboard = keyboardfactory.createStringKeyboardInput();
	
	
	
	textField.addMouseListener(new MouseAdapter() {
		@Override
		public void mousePressed(MouseEvent e) {

			final JTextField text = (JTextField) e.getSource();

			keyboard.show(text, new KeyboardInputCallback<String>() {
				@Override
				public void onOk(String value) {
					text.setText(String.valueOf(value));
				}
			});
		}

	});
	
	textField = new JTextField();
	textField.setBounds(132, 296, 530, 110);
	getContentPane().add(textField);
	textField.setColumns(10);

```

Thank you very much in advance for your help and time!

---

<div class="post-metadata">

**Author:** ![anon87732236](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@anon87732236](https://forum.universal-robots.com/u/anon87732236)\
**Post date:** [April 28, 2021, 2:17pm UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/7 "2021-04-28T14:17:09Z")

</div>

Hi @anon47255888,

Have a look at [this](https://github.com/UniversalRobots/MyDaemonSwing) sample, more specifically at the _MyDaemonProgramNodeContribution.java_ file. You should get a good idea on how to correctly use the apiProvider.

---

<div class="post-metadata">

**Author:** ![anon95815247](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon95815247](https://forum.universal-robots.com/u/anon95815247)\
**Post date:** [April 29, 2021, 6:41am UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/8 "2021-04-29T06:41:57Z")

</div>

Hi alexander,

Thank you for you idea, I will take a look and come back with a feedback.

Thanks!

---

<div class="post-metadata">

**Author:** ![fujikit](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/fujikit/32/6042_2.png) [@fujikit](https://forum.universal-robots.com/u/fujikit)\
**Post date:** [April 29, 2021, 2:18pm UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/9 "2021-04-29T14:18:29Z")

</div>

I have the sample for creating custom dialog with keyboard feature.

Please refer here too.

> **[GitHub - t-fujiki/com.takaaki.urcap.dialogscript2](https://github.com/t-fujiki/com.takaaki.urcap.dialogscript2)**
>
> Contribute to t-fujiki/com.takaaki.urcap.dialogscript2 development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![anon47255888](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@anon47255888](https://forum.universal-robots.com/u/anon47255888)\
**Post date:** [April 30, 2021, 5:52am UTC](https://forum.universal-robots.com/t/joptionpane-keyboard/6783/10 "2021-04-30T05:52:13Z")

</div>

Hello ,

Thank you very much for your help. Finally I have found a working solution just by copying from your examples from Contribution and View all the things regarding keyboard.  
It seems that my mistake was I did not include in the Service class the API Provider.  
Maybe if someone has some time can please explain to me in simpler terms,why do we need to have 3 separate files for un Urcap? I saw some examples with just one file, how they are build?  
I have very little to no experience in OOP.

Thank you very much again for the time and the help!
