jQuery in Action, 2nd edition*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes ActionPerformed happens twice... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "ActionPerformed happens twice..." Watch "ActionPerformed happens twice..." New topic
Author

ActionPerformed happens twice...

Tina Ljuslin
Greenhorn

Joined: Jun 08, 2001
Posts: 25
Hi, can anyone please help me with this
I have a controller class that looks like this:
public class FBNController implements ActionListener{
private FBNInterface data;
private FBNClientGUI gui;

public FBNController(FBNInterface fbn){
data = fbn;
gui = new FBNClientGUI(this);
.......
public void actionPerformed(ActionEvent ae){
...
and a GUI class:
public class FBNClientGUI extends JFrame{
private ActionListener al;
public FBNClientGUI(ActionListener al) {
this.al = al;
......
findMenuItem.addActionListener(al);
....
and all the actions are performed twice. does anyone know why?
And is it a good way to have the controller as an listener to the GUI?
Please help me!
// tina
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Tina,
Somewhere in your code, you are adding the action listener twice. I suggest using the controller as follows:
1) Pass the controller instance to the gui contructor
2) Using set methods in gui, add the listeners from the controller constructor
3) Have a seperate inner class in the controller to listen for ActionEvent. In other words, there will be a seperate ActionListener instance listening for every ActionEvent.
Tina Ljuslin
Greenhorn

Joined: Jun 08, 2001
Posts: 25
Yes, i got it to work, stupid mistake.
But why is it better to use inner classes then having the method actionPerformed(..) and in there check what happened?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

1) Pass the controller instance to the gui contructor
2) Using set methods in gui, add the listeners from the controller constructor
3) Have a seperate inner class in the controller to listen for ActionEvent. In other words, there will be a seperate ActionListener instance listening for every ActionEvent.

#1, so now the GUI knows about the controller?
It should be the other way around. Controller should know about the GUI, but GUI should know nothing about anything but displaying GUI. When I say that, the GUI also allows adding ActionListeners.
I think it is a little bit confusing. I think you have it right, but something is amiss in how you described it. Statements 2 and 3 are absolutely correct. But you can expand on 3 by making the Inner Class call a method in the Controller, rather than inside the Inner Classes ActionPerformed. Even though you won't need it here, it would allow calls to the same code from other sources other than the ActionPerformed method.
Mark
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
Mark,
You are right about the Mediator pattern. GUI doesn't have to know about the controller. In my design, I have a JFrame, JTabbedPane and couple of JPanels. JFrame and individual JPanels have their own controller.
When the user wants to view the details of a flight from the search screen, search panel controller talks to JFrame controller to show the details panel.
I've created a generic method in JFrame controller to create the details JPanel and set the data. Below is the code:

In the code above JFrame is the ReservationUI and the controller for JFrame is ReservationUIController
ReservationPanel is an abstract class implemented by the search JPanel and the flight details JPanel.
You can see the getController() method that I invoke on the JPanel. This is only reason why my JPanel needs to know it's controller.
Mark, I really like to hear from you.Thanks.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OK, I see how you are doing that. I myself had two JPanels and a JFrame, however, I didn't need to have a controller for each. The assignment is just to simple and basic to add that extra "Functionality".
I have seen many people here go overboard and put too much into the design. It's like you have a 1/4 Acre Lot and you want to build the Taj Mahal on that lot. You don't need to go through all that fun and frustration when you don't need to.
Don't get me wrong. I like your design, but it's a little overkill. And sometimes that intimidates a Junior Programmer.
I myself added a little extra when I extended the
PlainDocument class to make a JTextField only accept positive numbers. But that as far as I went.
I want you to be able to get the highest points, and I know you are a really good programmer, as I can tell from your posts helping other people. So good luck.
Mark
[ April 15, 2002: Message edited by: Mark Spritzler ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionPerformed happens twice...
 
Similar Threads
help with actionPerformed method.
How flexible should the MVC implementation be?
Please comment on FBN design
actionListener help w/sample code
populate data in text fields after selecting combo box list