<< Chapter < Page Chapter >> Page >
Java GUI programming including basic event handling, adapters, and the command and null-object design patterns.

1. jframe with jbuttons and event handlers (frame2.java)

Source code for Frame2.java and the associated Frame2App.java is at the end of this module, or download the entire archive

Command design pattern

When a user clicks on a button, an ActionEvent object is delivered to the button. In order for the button to perform an application specific task, we must register an ActionEventListener to the button and program this event listener to perform the task we want. The ActionListener class is an example of what is called a " command " from the Command Design Pattern . The code to add an ActionListener to a button looks like:

myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {// code to perform a task goes here... }});

The example Frame2 (source code at end of the module) shows how to dynamically change the layout manager for a JFrame by calling its setLayout(...) method resulting in a dynamic re-arrangement of the GUI components in the JFrame .

2. jframe with jbuttons and adapters (frame3.java)

Source code for Frame3.java and the associated IView2World.java , Frame3controller.java and Frame3App.java is at the end of this module, or download the entire archive

Frame3 illustrates how to decouple the view from the rest of the world by having the view communicate to an interface called "adapter". A controller object, Frame3Controller connects the view Frame3 to the world by installing a concrete adapter into the view. The adapter is instantiated as anonymous inner object and has access to all of its outer object. The view does not and should not know anything about the world to which it is connected. This adds flexibility to the design.

Null-object pattern

In much of the current programming practice, the special value null is often used as a flag to represent a gamut of different and often disconnected concepts such as emptiness and falseness. This can result in a lot of confusion and complex control structures in the code. In our view, null should only be used to denote the non-existence of an object. null is not an object and has no "intelligence" (i.e. behavior) for other objects to interact with. Therefore, whenever we work with a union of objects and discover that one or more of these objects may be referencing a null value, we almost always have to write code to distinguish null as a special case. To avoid this problem, we should think of adding a special object, called the null object , to the union with appropriate behavior that will allow us to treat all object references in a consistent and uniform way, devoid of special case consideration. This design pattern is called the null object pattern. We have used the null object pattern in one occasion: the EmptyNode to represent the empty state of a (mutable) list ( LRStruct ).

In Frame3 , the view adapter, _v2wAdapter , is initialized to an (anonymous) IView2World object. It is there to guarantee that _ v2wAdapter is always referencing a concrete IView2World object, and , since setV2WAdapter(...) only allows a non-null assignment, we can always call on _ v2WAdapter to perform any method without checking for null .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask