Over a million developers have joined DZone.

Customizing the NetBeans Platform Main Window: When & How

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

The main window of the NetBeans Platform is a frame that can be obtained from the NetBeans Window Manager. Once you have it, you can do several things with it, such as change the main window's default size at start up.

But, before looking at that, where is the best place to put code that should do something with the main window at start up? You have two places:

  • org.openide.modules.ModuleInstall. Several examples on the web (mine) use this approach. By overriding the 'restored' method in the ModuleInstall class, you can affect what happens when a particular module is installed. Derived from this, these instructions, for example, show how to set the main window size.

  • WarmUp Tasks. However, take a look at org.netbeans.core.ui.warmup.MenuWarmUpTask instead. Here we have a plain Runnable. What you don't see is that in the layer.xml file of the related module, the Runnable is registered within the 'WarmUp' folder. As stated in the comments of this blog entry, ModuleInstall.restored is dangerous when trying to "access the main window from there. It is run when the splash screen is showing. By accessing the main window too early something might not be ready (yet). This is very risky approach - use WarmUp task or simple action to activate it." For details, read this document which includes a discussion about the WarmUp task: "The tasks are executed just after the IDE starts (main window is shown) in a low priority thread."

Therefore, below we will use the WarmUp approach instead. Here is my Runnable, based on Charles Ditzel's The Vibrating JFrame tip:

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.openide.windows.WindowManager;

public class DemoWarmUpTask implements Runnable {

public void run() {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
Frame f = WindowManager.getDefault().getMainWindow();
f.addWindowListener(new Listener());

class Listener extends WindowAdapter {

public void windowActivated(WindowEvent event) {
JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
final int originalX = frame.getLocationOnScreen().x;
final int originalY = frame.getLocationOnScreen().y;
try {
for (int i = 0; i < 20; i++) {
frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y + 10);
frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y - 10);
frame.setLocation(frame.getLocationOnScreen().x - 10, frame.getLocationOnScreen().y);
frame.setLocation(originalX, originalY);
} catch (Exception err) {


Next, put this into the layer:

<folder name="WarmUp">
    <file name="org-yourorghere-warmuptaskdemo-DemoWarmUpTask.instance"/>

Now run the module and whenever the main window of the application is activated, it will vibrate. In addition to making the main window vibrate, you can also change its size, as indicated above. Similarly, you can change the main window's shape as well.


The Java Zone is brought to you by Stormpath—a complete, pre-built User Management API. Want to learn how to use JWTs to protect microservices from CSRF and more? Check out this on-demand webinar with our Java Developer Evangelist, Micah Silverman.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}