Over a million developers have joined DZone.

Customizing the NetBeans Platform Main Window: When & How

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

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 {

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

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

class Listener extends WindowAdapter {

@Override
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++) {
Thread.sleep(10);
frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y + 10);
Thread.sleep(10);
frame.setLocation(frame.getLocationOnScreen().x, frame.getLocationOnScreen().y - 10);
Thread.sleep(10);
frame.setLocation(frame.getLocationOnScreen().x - 10, frame.getLocationOnScreen().y);
Thread.sleep(10);
frame.setLocation(originalX, originalY);
}
} catch (Exception err) {
err.printStackTrace();
}
}
}

}


Next, put this into the layer:

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


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 in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

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

{{ parent.tldr }}

{{ parent.urlSource.name }}