Nov 212012
 

Today I was working again on the undocked TopComponent that inspired the post Hiding Tabs for Undocked TopComponents, this time adding a custom statusbar to it. And in the process I found a way of adding a banner to a NetBeans Platform application. There might be unexpected side effects with layout, but so far it looks good. :) Here is what my sample application looks like, to explain what I mean by ‘banner’ (the green part in the screenshot).

Sample application with banner

Sample application with banner

Well, as far as I gather the idea is not brand new. I had a hint from a forum thread that helped to point me in the right direction. Toni mentions there that you can add components outside the control of the window system, but the blog post linked to there no longer exists…

I started by browsing through the GUI elements in debug mode. And I soon found out that by calling

((JRootPane) WindowManager.getDefault().getMainWindow().
        getComponents()[0]).getContentPane()

you can find the main panel that contains all the GUI elements. And this panel has a BorderLayout. So lets look at the different parts of the NetBeans IDE as contained in this window to illustrate.

NetBeans IDE showing layout areas

NetBeans IDE showing layout areas

I created a normal JPanel form (I called mine BannerPanel) and added the components that you see on the screenshot to it. Then I created an installer class for the module, and into the restored method I put the following code:

WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
    @Override
    public void run() {
        Frame mainWindow = WindowManager.getDefault().getMainWindow();
        Container contentPane = 
                ((JRootPane) mainWindow.getComponents()[0]).getContentPane();
        BannerPanel banner = new BannerPanel();
        contentPane.add(BorderLayout.NORTH, banner);
        ((BorderLayout) contentPane.getLayout()).
                invalidateLayout(contentPane);
        mainWindow.validate();
    }
});

Now you will notice that I am putting the panel into BorderLayout.NORTH, which according to the screenshot earlier is where the toolbar already is. What is in fact in that space is a container panel that keeps the space for the toolbar. So replacing it means that the panel actually starts at the same height as the toolbar. And this means that you need to leave some open space at the top of the panel that you design. See the screenshot below showing my very green form in the GUI designer.

Banner in GUI designer

Banner in GUI designer

One last gotcha. You have to specify a height for the form by re-sizing it with the GUI designer. Otherwise the form will end up being small enough to be hidden behind the toolbar.

And remember – this is a way to bypass the NetBeans Platform window system to add components around it. So if things go wrong, you will have to go on a quest to find a Swing guru… :)

  3 Responses to “Adding a Banner to a Platform Application”

  1. Great post. I think Geertjan did something similar: https://blogs.oracle.com/geertjan/entry/new_banner_mode_for_netbeans.
    It would be nice to see Geertjan’s aproach combined with your post “Hiding Tabs for Undocked TopComponents”.
    Thanks for sharing the code.

  2. Links:

    Have created an RFE derived from information in this blog. https://netbeans.org/bugzilla/show_bug.cgi?id=242278

    Also there’s a forum discussion here: http://forums.netbeans.org/topic59380.html

    And Geertjan has blogged about the same topic here: https://blogs.oracle.com/geertjan/entry/of_toolbars_and_banners

  3. I’ve refined the method you propose. You can read about it here.

    Unlike you I did not find the NB status bar to be in the SOUTH position so I haven’t found a way of doing a bottom banner using this method. Doesn’t matter for me as I only need a top banner.

 Leave a Reply

(required)

(required)


+ seven = 16

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>