Saturday Oct 04, 2008

"CTL_MainWindow_Title": Customizing the Title Bar

There are many ways to customize a NetBeans Platform application's title in the title bar. Below, I try to list all of them, together with all related references. One typical added requirement is that the title should change dynamically, upon selection of something different in the application. For example, in the IDE the current project's name appears in the IDE's title bar. That's also quite easy to do.
import org.openide.modules.ModuleInstall;

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        System.setProperty("netbeans.buildnumber", "");
    }

}

The above comes from an interview with Emilian Bold. The most often heard solution is to go here in an application's branding folder, in the Files window in the IDE:

In the above file you'll find keys/value like this:

CTL_MainWindow_Title=DemoApp {0}
CTL_MainWindow_Title_No_Project=DemoApp {0}

Simply remove the {0} at the end of the values and you'll not see a build number in the application.

Another approach is to grab the application's frame and change its title:

public class Installer extends ModuleInstall {

    @Override
    public void restored() {
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
            public void run() {
                JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
                String title = NbBundle.getMessage(Installer.class, "LBL_TITLE");
                frame.setTitle(title);
            }
        });
    }
    
}

The code above could be anywhere at all, not necessarily in the Installer class. Note that above we're referring to a bundle file that is in the same package as our class. In that bundle file, the value of the key "LBL_TITLE" (or whatever the name of your key is) sets the title of the application. This means you could have many different titles in the bundle and then dynamically switch them at runtime from the code, as done above.

However, there's more you can do with bundle files, as the code completion for NbBundle.getMessage indicates:

So, based on the current selection, you can pass something into the argument that will tweak the title of the title bar, as shown below. In this case, a listener is set on an Explorer Manager so that the currently selected node in an explorer view determines the content of the title bar:

em.addPropertyChangeListener(new PropertyChangeListener() {
    public void propertyChange(final PropertyChangeEvent evt) {
        WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
            public void run() {
                JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
                String title = NbBundle.getMessage(DemoTopComponent.class, "LBL_TITLE", em.getSelectedNodes()[0].getDisplayName());
                frame.setTitle(title);
            }
        });
    }
});

In the Bundle.properties file, which is in the same package as where the above class is found, I have the following entry:

LBL_TITLE=Selected: {0}

The {0} is a placeholder for the first additional argument sent by the NbBundle.getMessage in my Java code above. Now, whenever I choose a new node in the explorer view, the title changes in the title bar, as indicated below:

Up to 4 different objects can be passed into the argument of NbBundle.getMessage, only the first of which must be a string. The others could be any object, even an array of objects. For example, it could be like this in the Bundle.properties file:

LBL_TITLE=Selected: {0} {1} {2} {3} {4}

The whole value above is a string, so you could put other strings in between the 4 placeholders, such as shown here with some random characters thrown in:

LBL_TITLE=Selected: {0} -- {1} / {2} and {3} --- {4}

This is not useful only in the context of title bars, but is a good example of its applicability in that case.

Two other useful pieces for working with the title bar are Netbeans Platform Branding and version info and Branding custom version info into NetBeans RCP apps and the missing Bundle.Properties file.

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« October 2008 »
SunMonTueWedThuFriSat
   
2
7
8
11
12
20
24
25
27
31
 
       
Today