How to set focus

Hi,

Let say I want to chose wich component in a window would have to focus when the windows is open,

On the windows, I would edit the visionWindowOpenend

And I would like to know what is the name of the fonction to call so I can set te focus.

Also, I could use the same fonction to change the focus on the next text field when operator pressed Enter.

What is the name of the fonction to set focus on a specific component ?

Component.requestFocus() is what you need. It is a method on every component.

1 Like

One of the common component functions is .requestFocusInWindow(). See: https://docs.inductiveautomation.com/display/DOC79/Component+Scripting

From my experience, if you call this on a component when the window is opened, use invokeLater (either with the Advanced Settings on the Script Editor or in an invokeLater function call) to make sure bindings and other window opening events get done before the focus is set.

1 Like

Here is what I want to do :

From the “Database Manipulation” window, I click on “Edit” button to open the next window

Now when the “Edit inventory” window popup, I would like to put the focus on the cancel button.

Because, if the focus is give to the name textfield, the binding on the Text property does not occur, wich leave the field blank.

So, to solve that problem, I want that when the “Edit” window open, I would set to focus on the “Cancel” button, so that the name textfield git filled by the binding.

Is it possible ?

Ok, good for the requestFocusInWindow()

Now, what is the event that is call when the windows is creteated, or opened ?

Because I have tried this line on an other event, and work well :
system.gui.getParentWindow(event).getComponentForPath(‘Root Container.btnCancel’).requestFocusInWindow()

Now I just need the put it into the event that is called on the window opening…

Did you try using the visionWindowOpened event that exists on windows?

Yes, On me Edit windows, I have tried in the visionWindow, visionWindowOpened event

system.gui.getParentWindow(event).getComponentForPath(‘Root Container.btnCancel’).requestFocusInWindow()

and it is not working, the focus stay on the first textfield.

The only way I found for now, wich is not elegent, is to put a textfield above this textfield, so to focus goes into the first textefiedl… but I don’t like this “patch”…

On the Script Editor window at the bottom there is an Advanced Settings selection. Have you selected the “Invoke Later” checkbox?

Yes, it probably needs to be invoked later so that bindings don’t change the focus.

1 Like

I did try to invoke later, but did not work.

So, I have a script, on the Edit windows, event visionWindow/visionWindowOpened
and it is in invoke later, still, the focus does not go on the cancel buton.

Ok, got it…

The good event is internalFrame/InternalFrameOpened
and invoke later!

thanks to tell me about invoke later!

Ok, yes and no…

The focus goes on the buton buton Cancel, but a cursor stay in the first text field.

I tought that if we move the focus, the curose would go…

I have tried sevral thing to take out the cursor…

  • Name textfiedl transferFocus()
  • Name textfiedl nextFocus()
  • Name textfiedl transferFocusBackward()
  • Disable() and enabled() the Name textfield
  • buton Cancel requestFocus()

What is the name of the event called by “SHIFT + TAB” keys ?
Because, if I do SHIFT TAB, it does move the focus, and take out the cursor from the textfield…

I have the same problem.

The cursor stays in a text box and the tag binding changes when I change it from looking at generator 1 to generator 2, but the text box still shows the value from the generator 2 page.

I got a button to have the focus when the frame is activated but the cursor stays in the text entry field and the value in that field does not update.

Uncheck the ‘defer updates’ setting.

1 Like

This is the first item returned by searching "Ignition text field set focus" on Google so I think it's appropriate to add here. When the methods above didn't work for me I realized I was using a Text Area, not a Text Field. The script provided at Set focus on any input control was useful for finding out that for a Text Area the request focus script is really

getComponent('Text Area').components[0].components[0].requestFocus()

This sets the cursor at position 0. I wanted to set it at the end of the text, so I ended up using

c = getComponent('Text Area').components[0].components[0]
c.requestFocus()
c.setCaretPosition(len(c.text))
1 Like