<< 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 .

Questions & Answers

start new n questions too
Emmaunella Reply
summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
definition of sociology of education
Emmaunella
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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