GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes how to call a method in method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to call a method in method?" Watch "how to call a method in method?" New topic
Author

how to call a method in method?

Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
Hi people, I tried to place a method in actionperformed.. but I had trouble.. and somewhere I read that its impossible.. so is it impossible to code as below :

public void actionperformed(ActionEvent event) {

if (event.getSource.equals(anybutton)) {method1;}
else
if (event.getSource.equals(anotherbutton) {method2;}
else
.
.
.
}

Somewhere in tutorials its read that to be able to use method in method you must pass an object as parameter and THEN use the methods of THAT object..... but for actionperformed method its impossible, for the object parameter is (ActionEvent event) itself and I cant create a class for the event and place the methods in it.
Just help! I dont know what to do at this point...
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Which method are you trying to call? I get the idea that you're after a way to search for the correct object to be able to reach a method.


Java hobbyist.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
Two things, one minor the other major.

The problem you are having is a minor syntactic one. If the name of your method is method1 and it takes a parameter, you can simply write the name of the method with the parameter in (). Like this:

method1(123);

if it takes an int parameter, in which case it is 123. Your method1 doesn't seem to need parameters, so you can simply write

method1();

***************************************************************************

There is a more serious problem; even though many books show the addActionListener(this) syntax, you can see you end up with an actionPerformed() method which is full of so many if-elses that it becomes difficult to understand. There is frequent discussion of that problem on the Swing forum (eg here: and look at the older links quoted, or maybe this might be a better link.
Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
When you were writing replies I as well found out that its syntactical, yes, and when I placed (), it worked.. Thank you both

And Ronald, actually just because I thought that I wouldnt be able to use method in method without an object parameter, I was,yes, looking for a proper object parameter... I would get in a wrong way.. but now its ok..

Thanks again
Someday, will I be good for others as well? Hope so.. its hard all the time to get some things from people.. I will join you two someday, God willing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072
    
  23
Originally posted by Gulsum Ramazanoglu:

Someday, will I be good for others as well?
Yes
 
GeeCON Prague 2014
 
subject: how to call a method in method?