Check which component has focus

Hi Guys,

How can I check which component currently has focus?

Regards,

Dion

from java.awt import KeyboardFocusManager
focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()
1 Like

Does this give the path to the component? If I try to print focusedComponent to the console, it returns ‘None’

Sorry, I was doing something wrong… Ok, printing focusedComponent to the console returns this:

com.inductiveautomation.factorypmi.application.components.PMITextField[Username,89,43,149x22,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.CompoundBorder@6e45d33d,flags=296,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=100,height=22],caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=147,g=141,b=130],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=1,left=1,bottom=1,right=1],selectedTextColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],selectionColor=javax.swing.plaf.ColorUIResource[r=250,g=214,b=138],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

Is this a string? Doesn’t seem like it, I need to extract the name of the component - in this case - Username.

1 Like

What’s happening there is print is calling out the PMITextField's implementation of toString - a Java method that just asks the class how to render itself as a string.

focusedComponent.getName() or just focusedComponent.name would get you a reference to the defined name.

1 Like