This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes GUI, actions and controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "GUI, actions and controller" Watch "GUI, actions and controller" New topic

GUI, actions and controller

Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 111

Hi all,
After having (nearly) completed the backend, i am working at the GUI now, and altready have a working version using the MVC architecture :-)

For invoking actions on the Controller, I see the following possibilities:

Method 1.
To every control in the View an anonymous ActionListener is added using
addActionListener in the following well-known way.

This is straight-on and works of course fine. But when we want to attach the action to more than one gui control, for example menu-items, it is better to use Actions (javax.swing.Action) to define caption, tooltip, and the actions performed in a uniform way. This brings us to method 2.

Method 2.
For each action, a separated CustomAction class is defined, extending the AbstractAction class. For example: class BookFlightsAction extends AbstractAction. The various propeties can be set in the constructor, and the actions in the implemented actionPerformed method.
I create those actions in the contructor of the Controller class and add a method to the Controller for each action:
public Action getCustomAction();

The action is attached to the control as follows:

The PROBLEM is that no parameters can be passed in as the action is created in the contructor of the Controller. I have solved this issue by passing in the JTable itself to the contstructor of the Controller, where the selected records can be retrieved using the getSelectedRecords() method.
But this makes the Controller dependent of the JTable component, which is no good design I think. I introduced more abstraction by having the GUI implement an interface containing methods that return the desired info from the JTable. I pass this interface in to the Controller constructor instead of the JTable itself.
Is this a well known way of doing ? Or are there other ways I haven't thought of yet... ?

Regards, Klaas
[ July 31, 2004: Message edited by: Klaas van Gelder ]

"What you don't know, can't help you"
SCJP (81%), SCWCD (81%), SCJD (354/400), SCBCD (85%)
Klaas van Gelder
Ranch Hand

Joined: Jul 08, 2004
Posts: 111

maybe this interface is the same as what some people use a "hook" to the GUI...
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
>>Is this a well known way of doing ?

In the archives, I�ve seen an example where the Controller knows the View and the Model. Only the View knows the JTable and the TableModel.

View <---- Controller ----> Model

The Controller creates a listener and gives it to the view in a callback. Sort of like this.

[ August 02, 2004: Message edited by: Marlene Miller ]
I agree. Here's the link:
subject: GUI, actions and controller
It's not a secret anymore!