DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Customizing the NetBeans Platform Main Window: When & How

Customizing the NetBeans Platform Main Window: When & How

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
Sep. 28, 08 · Java Zone · News
Like (0)
Save
Tweet
12.35K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

NetBeans

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Chopping the Monolith
  • How BDD Works Well With EDA
  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo