Geertjan's Blog

  • August 27, 2010

Remember To Buy Dinner On The Way Home

Geertjan Wielenga
Product Manager
The best thing about giving NetBeans Platform Certified Trainings is the cool challenges that students leave you with. In the process, you learn about actual business needs and you teach yourself how to implement them on the NetBeans Platform.

A great example of this during the Johannesburg training (which is now complete, after 5 days, i.e., 3 days basic training, followed by 2 day advanced course) was posed by Marcel Auret and Kobus Botha from Saab. During the presentation about the NetBeans window system, they said they need a window that:

  • is always on top
  • opens undocked
  • is modal
  • cannot be closed

Why would such a window be needed? For warning messages, or notifications, as shown below:

So, maybe a JDialog could be used instead, you might wonder. However, an additional requirement was that the window should be dockable. I.e., integration with the NetBeans window system is needed.

So, here is the new mode I created:

<?xml version="1.0" encoding="UTF-8"?>
<mode version="2.3">
<name unique="warning" />
<kind type="view" />
<state type="separated" />
<path orientation="horizontal" number="100" weight="0.5"/>
<bounds x="713" y="150" width="467" height="217" />
<frame state="0"/>
<empty-behavior permanent="true"/>

Note that "view" above will cause the window to be modal, while "separated" will cause it to be undocked.

Then I created a TopComponent and registered it in the layer in the above new position, which also needs to be registered in the layer.

I created an installer, with this "restored" method, which will be called when the module is installed:

public void restored() {
WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
public void run() {
final TopComponent tc = WindowManager.getDefault().findTopComponent("WarningTopComponent");
JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
frame.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
public void windowLostFocus(WindowEvent e) {

The constructor of the TopComponent has this in its constructor:

putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);

As well as this:

public boolean canClose() {
return false;

And now everything works as expected. Right-clicking on the window's tab reveals the "Dock/Undock" menu item which, when invoked, causes the window to be docked when needed. Whatever happens (except when docked, of course), the window will stay on top and will not be closeable (even when the small red button is clicked, which can remain as decoration of the fact that this is a warning window).

Be the first to comment

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