Add a Datapicker to the Netbeans Swing Controls Palette

For small Swing applications, I like to use the Netbeans GUI Builder.  The problem is that there is no date picker.  And today, I really needed a date picker.  Fortunately, there is a solution.  Remember SwingX?  It’s built into Netbeans, and it has a good date picker called JXDatePicker.  Here’s how to get it on your Swing Controls palette so that you can drag it onto your form.

By “palette”, I mean the view that pops up on the right side of the IDE when you use the GUI Builder.  Notice that the Swing Controls palette doesn’t have a date picker of any sort (shame on you Netbeans! This is basic stuff!).

The goal is to get JXDatePicker onto that palette.

1.) Pull up the Palette Manager for Swing/AWT Components.

2.)  Click “Add from JAR”

3.) Browse to [NETBEANS HOME]\ide\modules\ext and select swingx-0.9.5.jar

4.)  This will bring up a list of all the components available for the palette.  Lots of goodies here!  Select JXDatePicker.

5.)  Select Swing Controls

It will immediately show up on your Swing Controls palette.  All that’s left is to drag onto form!

Netbeans: Session Beans for Entity Classes

Netbeans really hit a home run when it automated the creation of session beans from entity classes.  It also does a good job creating entity beans.  It’s a win-win (God!  I can’t believe I just wrote the phrase win-win).   It makes good use of the facade design pattern and gives you a service that you can inject into your controller.

Generated Entity Beans

First, let’s talk about what you get when you let Netbeans generate your entity beans.  You might say, “Well Mr. RAJP, I’m perfectly capable of creating my own damn entity beans.”  To which I say “Fiddlesticks.  You’re wasting your employer’s valuable time!”

The entity bean that Netbeans generates gives you everything you need (and perhaps more) just by selecting a table in a list.  Netbeans generated entity bean come with the following features:

  1. Declares each field in the table with an estimation of the proper type and annotates maximum sizes, default values,  whether it can take a null value, and whether it is the primary key.
  2. It maps all of the database relationships with @OneToMany, @ManyToOne, and @ManyToMany depending on how well you designed your database.
  3. It creates a nested array of @NamesQueries searching on each field.
  4. It generates all the getters and setters
  5. It overrides hashCode() , toString(), and equals()

The only modifications I ever have to make is to add a sequence generator to the primary key and add a few more named queries.

“But David! This is too easy.  Shouldn’t I have to work harder to get my job done?”

“No.  This gives you more time to build your damn system.  Now swallow your pride and code!”

Generated Session Beans for Entity Beans

This is where things get really cool.  For years I’ve been coding a service class, and doing it poorly.  Being a lowly RAJP (Regular Average Java Programmer), I was building a static service of operations to perform on the entity beans.  Basic CRUD plus a few custom business functions.  Did it work?  Yes.  Should I have created some sort of service that could be injected into my controller?  Yes.  Am I too lazy to do anything that cool?  Definitely.

When I upgraded to Netbeans 7.1, and was generating some entity beans, I happened to notice an option to generate “Session Beans for Entity Classes…” in the New menu.  Honestly, I had no idea what this would do.  When I did it though, which was as simple as selecting from a list of entity beans, it gave me something totally cool.  It gave me a set of stateless EJB classes replete with EntityManagers already hooked up to my persistence unit.  But wait!  There’s more!  It gave me a nicely designed AbstractFacade for the EJBs to extend with all the CRUD methods I would need.  It’s so nicely designed, in fact, that it can be extended by any session bean you generated from an entity.  It’s all generical and stuff.

Netbeans got it right!  This is a far better a design than what I could have produced myself.  This is a design worthy of a JE (Java Expert).

The only slightly weird thing is that the session bean never uses the named queries in the entity bean.  It handles all of it’s operations with JPAs CriteriaQuery class.  But that’s just fine with me.  When I need it, I just add another operation to the session bean to call named queries.

Now I have a set EJB facades that I can inject into my controllers.  I use Stripes ActionBean for my controllers, by the way.  It’s a kick ass stack.  JSP, Stripes, EJBs, JPA, Glassfish, and Netbeans.

Netbeans team, you NAILED this down TIGHT! Yessir!

How do I change the JDK home for Netbeans?

I recently upgraded my JDK and I started getting this message when I launched Netbeans.

Cannot locate java installation in specified jdkhome: [some outdated path].  Do you want to try to use default version?

I’m capable of ignoring messages like this for months, but when I tried to start a new Maven web application, it balked.  It’s simple enough to fix, but it wasn’t obvious to me.  So here you go.

Step One

In your Netbeans home directory (for example, C:\Program Files\NetBeans 7.0.1), open up the netbeans.conf in the etc directory (C:\Program Files\NetBeans 7.0.1\etc\netbeans.conf).

Step Two

Theres a property called netbeans_jdkhome.  Change the value to match the JDK you want Netbeans to use (for example, C:\Program Files\Java\jdk1.6.0_25).

Step Three

Save your changes

Step Four

Restart Netbeans

Handy dandy Netbeans feature: Print to HTML

In blogging, I often like to share some code.  Often times, I’ll just take a screen print, crop it, and upload it.   But yesterday I found another way to put formatted code in my posts using Netbeans:  Print to HTML.  It’s under the File menu.

  1. Open your source in Netbeans
  2. Click Print to HTML under the File menu
  3. Choose between “Print to File” and “Print to Clipboard
  4. Click the HTML tab (assuming you’re using WordPress
  5. Paste in markup into your blog editor…et voila!


 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 6 package sample.transfer;
 8 import java.math.BigDecimal;
10 /**
11  *
12  * @author david ctr wilson-bur
13  */
14 public class ProductTO {
15     private Integer productId;
16     private String description;
17     private BigDecimal purchaseCost;
19     public String getDescription() {
20         return description;
21     }
23     public void setDescription(String description) {
24         this.description = description;
25     }
27     public String getManufacturerName() {
28         return manufacturerName;
29     }
31     public void setManufacturerName(String manufacturerName) {
32         this.manufacturerName = manufacturerName;
33     }
35     public Integer getProductId() {
36         return productId;
37     }
39     public void setProductId(Integer productId) {
40         this.productId = productId;
41     }
43     public BigDecimal getPurchaseCost() {
44         return purchaseCost;
45     }
47     public void setPurchaseCost(BigDecimal purchaseCost) {
48         this.purchaseCost = purchaseCost;
49     }
50     private String manufacturerName;
54 }