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!

Advertisements

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!


1
/*

 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5
 6 package sample.transfer;
 7
 8 import java.math.BigDecimal;
 9
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;
18
19     public String getDescription() {
20         return description;
21     }
22
23     public void setDescription(String description) {
24         this.description = description;
25     }
26
27     public String getManufacturerName() {
28         return manufacturerName;
29     }
30
31     public void setManufacturerName(String manufacturerName) {
32         this.manufacturerName = manufacturerName;
33     }
34
35     public Integer getProductId() {
36         return productId;
37     }
38
39     public void setProductId(Integer productId) {
40         this.productId = productId;
41     }
42
43     public BigDecimal getPurchaseCost() {
44         return purchaseCost;
45     }
46
47     public void setPurchaseCost(BigDecimal purchaseCost) {
48         this.purchaseCost = purchaseCost;
49     }
50     private String manufacturerName;
51
52
53
54 }
55
56

Using the Java Persistence API with a Visual JSF Web Application

I just finished trying out the Using the Java Persistence API with a Visual JSF Web Application tutorial. It’s pretty straightforward and easy to follow. The effect was positive. I was not immediately successful at runtime.

Here’s the problem I encountered:

In the section “Creating a Java Persistence Entity Class Representing the Users Database Table”, either a step was left out, or there was some sort of glitch. This is what my Persistence Units (persistence.xml) screen looked like after I completed this step:

fig9.jpg

Note that the “Include Entity Classes” box is empty. When you try to run the web app, you get the following error:


Caused by: Exception [TOPLINK-8034] (Oracle TopLink Essentials - 2.0 (Build b58g-fcs (09/07/2007))): oracle.toplink.essentials.exceptions.EJBQLException
Exception Description: Error compiling the query [select c from Users as c]. Unknown abstract schema type [Users].

The solution is to open persistence.xml (Project–>TestModelApp–>Source Packages–>META-INF–>persistence.xml) and click the “Add Class” button. Select com.samples.model.Users. Rebuild. Redeploy.

The JPA design is a bit of an adjustment to me. I’m just getting used to seeing annotations in my beans (for Java Web Services), and now I have to adjust to seeing SQL code. It’s like the Data Access Object(DAO) and the bean are combined in one class. It’s different, but I’m not sure if it is good, bad, or just…well, different. I’m curious about how this will look in more complex data models where one bean may contain references to several other beans as instance variables. Just how many database transactions will it take to build one of these? As I’m writing this, it’s occurring to me that the answer is, “Well, just as many as there would be if you had written the JDBC code yourself, dumbass!”. Perhaps that’s true.

The main thing to take away from this is that

  1. I didn’t use a DataProvider (proprietary to Netbeans)
  2. I was able to bind the Persistence Unit in the visual designer (not drag n drop, but good enough)
  3. My data access (Persistence Units) are reusable
  4. Although I’m not sure if the JPA classes constitute a separate tier, at least they’re kept in a separate Netbeans Project so that they can be freely associated with other applications without duplication.

This is positive. I may even like it.

Some really good Visual Web JSF links

Dr. Winston Prakash is a Senior Staff Engineer at Sun Microsystems and works on Visual Web in Netbeans (formerly known as Project RAVE and Java Studio Creator). I’ve run into 3 really good pages that have this guy’s name on them:

Understanding Netbeans Visual JSF Web Application

This is a great article about what Netbeans Visual JSF does for you in your JSF programming. If you’re already familiar with JSF and you want to understand what Visual JSF is actually doing, then you need to check this out from the Visual Web guru. It gives a basic summary of the technology and it shows details about what code gets generated, what libraries get included, what happens at run time, and understanding the life cycle of a managed bean.

If you are interested in reading the minds of the Visual JSF team to see what you might expect in the future, read this wiki page:

Architectural Changes for Visual Web Next

Reading it is a similar experience to when I was a kid reading the Holiday edition of the Sears catalog and circling the things I wanted from Santa with a crayon. But just as the bold red disclaimer at the top of the wiki page points out, Santa makes NO PROMISES.

wishbook.jpg

And finally, Using the Java Persistence API with a Visual JSF Web Application . I will spend my work day stepping through this tutorial and telling you what I think. It was just one of those golden titles that showed up while I was googling for answers. I haven’t used JPA yet, but I’m sure interested in it. It piqued my interest at both the Javaone and Oracle OpenWorld conferences last year.

Visual Web JSF and data: questions, considerations, thoughts

If you arrived at this blog post hoping to find answers you will be cursing me within the next 30 seconds. This is a post for questions and snarkicism. In evaluating Netbeans Visual Web JSF and Woodstock, I am now turning my attention to data.

This is a touchy issue with me. I am hard-core about separation of concerns. I’m like the kid who will NOT eat the macaroni and cheese if the spinach is touching it! I don’t want any data access code touching my presentation layer!!! NO TOUCHY! This is why I cannot use the primary data binding method in Netbeans: dragging a table to a data control. Sorry. Can’t do it. I did not bust my butt learning java only to return to Powerbuilder-style data binding. Yo! Shoutz to the Powerbuilder Datawindow!! You rocked the 90’s! The Datawindow was one of the greatest software components of all time! But the days of throwing data access, business logic, and presentation into one bucket are gone. Oh, and I’m sure I’ll hear from some of my Powerbuilder friends spouting off about how NVOs (Non-Visual Objects) and Stored Procedures solve these design issues (almost as lame as my Perl friends who spout off about just how Object-Oriented Perl is now…right, like a Perl programmer cares about object-oriented design)…but anyway…I don’t like the idea of binding database tables to GUI components.

Perhaps I’m misunderstanding this whole thing. Perhaps the DataProvider object does constitute a separation of concerns. Perhaps it also promotes reusability. Further research is necessary for me to determine this. All I know at the moment is that the database programmer/DBA wants to back this thing with Oracle Stored Procedures, and you can’t drag a Stored Procedure onto a control. You get three choices (as far as I can tell): Drag a Table, Drag a View, and Drag a Web Service. I don’t see JPA, Beans, EJB, or Stored Procedures. If you want to use those, I guess you need to give up the nifty drag n drop data binding features and code it your damn self!

The following Q&A from an Ask the Experts for Netbeans 6 is relevant to my post:

question:

Michele Giuliano: I’m trying to convert my erp (written in Powerbuilder) in Java using NetBeans 5.5.1 and Visual Web Pack. I have used CachedRowSetXImpl and CachedRowSetDataProvider to manage the da develop with visual web pack. Is it right?

answer:

David Botterill: There are really lots of background questions that need to be asked to get the correct answer for this. But, given the information at hand, EJB is a better choice. If you are building an ERP, you will have lots of business logic that needs to live somewhere. If you go from a web client through the visual web dataproviders and CachedRowSet, you really only have a two-tier architecture, (client)-+-(data). In your case, you really need at least one other “middle” tier that contains the business logic for your system. The business logic is the value of your system and needs to be reusable and easily maintainable. This is the purpose for component-based architectures like EJB. You will thus have a (client)-+-(business logic)-+-(data) architecture. This allows you to change out the client while preserving your business logic. You CANNOT capture complex business logic like ERP in data access routines via dataproviders and CacheRowSet. Even the JSF backing beans will not really allow your business logic to remain autonomous since you’ll usually have framework-specific code living there. Another advantage of using this architecture is the ability to use something like Java Persistence API (JPA) on in the data tier and also this will allow you to expose your business logic via web services.

Perhaps I truly can’t bake my cake and eat it, too…at least not yet. This little nugget from the Architectural changes for Visual Web Next gives me hope for the future:

Data binding enhancements

Dataprovider is one of the main mechanism to bind data to the components. Eventhough, dataprovider is nothing but a thin API to support uniform way for the components and DesignTime System to interact, it can be consider proprietary, since it introduces “non standard” API with package name “com.sun.rave.data.*” in the source. It may be possible to hide the dataprovider, so that the user need not have to know about the dataprovider externally. This is already accomplished for table component.May in the future, we should

  • Promote Java Persistence API as the main data access mechanism
  • The DesignTime System must be able to bind collection of POJOs to components
  • When database table is drag and drop bind using JPA than CachedRowset
  • Complete designtime support to create Entity Bean and corresponding Controller for binding

Finally, we must get rid of the need for a live database connection even after the table is bound to the component. One solution for this is to use only JPA for binding. If Cachedrowset need to be supported for backward compatibility, then database schema should be persisted in the project for offline access later.

Let’s get busy with this one Netbeans!