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 modalcannot 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:
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: @Override public void restored() { WindowManager.getDefault().invokeWhenUIReady(new Runnable() { @Override public void run() { final TopComponent tc = WindowManager.getDefault().findTopComponent("WarningTopComponent"); tc.toFront(); JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow(); frame.addWindowFocusListener(new WindowFocusListener() { @Override public void windowGainedFocus(WindowEvent e) { tc.toFront(); } @Override public void windowLostFocus(WindowEvent e) { tc.toFront(); } }); } }); }
The constructor of the TopComponent has this in its constructor: putClientProperty(TopComponent.PROP_CLOSING_DISABL ED, Boolean.TRUE);
As well as this: @Override 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.


Read More about [Remember To Buy Dinner On The Way Home...