Geertjan's Blog

  • November 28, 2011


Geertjan Wielenga
Product Manager

Here's a very cool thing that I keep forgetting about but that Jesse reminded me of in the recent blog entries on Undo/Redo: "surviveFocusChange=true".

Look at the screenshot below. You see two windows with a toolbar button. The toolbar button is enabled whenever an object named "Bla" is in the Lookup. The "Demo" window has a "Bla" object in its Lookup and hence the toolbar button is enabled when the focus is in the "Demo" window, as shown below:

Now the focus is in the "Output" window, which does not have a "Bla" object in its Lookup and hence the button is disabled:

However, there are scenarios where you might like the button to remain enabled even when the focus changes. (One such scenario is the Undo/Redo scenario in this blog a few days ago, i.e., even when the Properties window has the focus the Undo/Redo buttons should be enabled.)

Here you can see that the button is enabled even though the focus has switched to the "Output" window:

How to achieve this? Well, you need to register your Action to have "surviveFocusChange" set to "true". It is, by default, set to "false":

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;
@ActionID(category = "File", id = "org.mymodule.BlaAction")
@ActionRegistration(surviveFocusChange=true, iconBase = "org/mymodule/Datasource.gif", displayName = "#CTL_BlaAction")
    @ActionReference(path = "Toolbars/Bla", position = 0)
public final class BlaAction implements ActionListener {
    private final Bla context;
    public BlaAction(Bla context) {
        this.context = context;
    public void actionPerformed(ActionEvent ev) {
        // TODO use context

That's all. Now folders and files will be created in the NetBeans Platform filesystem from the annotations above when the module is compiled such that the NetBeans Platform will automatically keep the button enabled even when the user switches focus to a window that does not contain a "Bla" object in its Lookup. Hence, the same "Bla" object will remain available when switching from one window to another, until a new "Bla" object will be made available in the Lookup.

Join the discussion

Comments ( 3 )
  • Jesse Glick Monday, November 28, 2011

    "keep the button enabled even when the user switches focus to a window that does not contain a "Bla" object in its Lookup" - almost. If the focus is switched to a window such as Services which _can have_ selections, but which does not happen to have a Bla in its current selection (either because that selection is empty or because it just contains something unrelated), then the button will be disabled. Surviving focus change just means the button will remain enabled if focus is switched to a window which _never has_ selections, such as the Output Window.

  • Ernest Tuesday, November 29, 2011

    That's interesting, it seems surviveFocusChange=true causes behaviour that is very close to that of StickyLookup (as if wrapping Utilities.actionsGlobalContext(), for a single TC, inside a StickyLookup).

  • j.boesl Tuesday, November 29, 2011

    Another useful blogentry to help everyone. Thanks a lot. But maybe you should add j.glick's explanation to your posting. Without it i was not able to understand it.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.