Tuesday, July 01, 2014

Specify a Block of Code to NOT Run in the NetBeans Form Designer

Sometimes, you will have code in the constructor of a Swing form that you do not want to execute when opening the form in the NetBeans form designer. I recently had this problem when working with Dependency Injection using Google Guice. Opening the form in the designer resulted in an exception with a call stack about 80 levels deep.

To avoid this problem, do the following:

Add the following import to your form:

import java.beans.Beans;

Surround any code you do not want to execute when opening the form in the NetBeans form designer in the following block:

        // Don't execute the following code in the designer
        if (!Beans.isDesignTime()) {
            // unexecuted code goes here

        }

No comments: