Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Customizing the NetBeans Platform Main Window: When & How

DZone's Guide to

Customizing the NetBeans Platform Main Window: When & How

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

 

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}