Adding Focus listener for a custom tool window.

Hi All,

I have a custom tool window which shows a table with some data. I need to refresh the window when the window gains focus, previously some other view was active. How can I add a focus listeners? Any pointers please.

Thanks
Devang Shah

10 comments

why is this different than adding a focus listener to your window (or component)? Is it a an independent window or a component in IntelliJ?

If it's a component in IntelliJ , then that component is probably available through some (getter) method found on some object with an association with that panel, right? Probably. At this point, what I personally do is go to the OpenAPI javadoc on my machine, load up the index and search in my browser for likely names - getComponent comes to mind) and start fishing around in there to see if I can find a good candidate for what I want.

0

Hi,

I create a component that extends JPanel. I register this using the ToolWindowmanager. I tried adding a class that implements FocusListener to this panel. When the window was restored, the folcus listener was not called.

Thanks
Devang Shah

0

what do the words the window refer to in your sentence?

0

Hi,

I am implementing a VCS plugin. I have a history view. Say, currently the view shows history for file A.java. Now I perform some operation on the file. After successful completion of the operation, I need to refresh the view. In my requirement, I should refresh the view only if it is active view, otherwise I should mark a refresh flag in the view. Now, when the view gains focus, since the refresh flag is set, new history will be fetched for the file.

My HistoryView class extends JPanel. I added a FocusListener to this panel. Now, when the view comes into focus, this listener should be invoked, however it is not working this way.

Any pointers how can I achieve this?

Thanks
Devang Shah

0

Hello Devang,

I am implementing a VCS plugin. I have a history view. Say, currently
the view shows history for file A.java. Now I perform some operation
on the file. After successful completion of the operation, I need to
refresh the view. In my requirement, I should refresh the view only if
it is active view, otherwise I should mark a refresh flag in the view.
Now, when the view gains focus, since the refresh flag is set, new
history will be fetched for the file.

My HistoryView class extends JPanel. I added a FocusListener to this
panel. Now, when the view comes into focus, this listener should be
invoked, however it is not working this way.

Any pointers how can I achieve this?


Why aren't you using the standard VCS plugin infrastructure? If you provide
an implementation of VcsHistoryProvider, you can reuse IntelliJ IDEA's file
history panel implementation, which includes the refresh possibility among
other features.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

hi.. still don't know what the word window referred to .. a JPanel with a FocusListener gets a FocusGained message but I think this comes from it's (containment) parent, so the question is, who is the containment parent?

At any rate, I would try to do what Dmitry was suggesting.

0

From the subject I would assume they meant the Toolwindow. And when
they say focus, I think they mean the toolwindow being shown rather than
specifically focused.

N.

softwarevisualization wrote:

hi.. still don't know what the word window referred to .. a JPanel with a FocusListener gets a FocusGained message but I think this comes from it's (containment) parent, so the question is, who is the containment parent?

At any rate, I would try to do what Dmitry was suggesting.

0

Thats right Nathan. Thanks for clarifying. Any idea how can I achieve this ?

0

Hi Dmitry,

I have some other custom windows besides History. How can I add a listener for window shown event?

Thanks
Devang Shah

0

Hello. Please try this code for your custom Tool Window:

      new FocusWatcher(){
        @Override
        protected void focusedComponentChanged(Component focusedComponent, @Nullable AWTEvent cause) {
          if (focusedComponent != null && SwingUtilities.isDescendingFrom(focusedComponent, toolWindow.getComponent())) {
            //update data here
          }
        }
      }.install(toolWindow.getComponent());

Also you can use ToolWindow.isVisible()

0

Please sign in to leave a comment.