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

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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