aspose file tools*
The moose likes Beginning Java and the fly likes Firing actionevents between classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Firing actionevents between classes" Watch "Firing actionevents between classes" New topic
Author

Firing actionevents between classes

Kari Nordmann
Ranch Hand

Joined: Feb 12, 2007
Posts: 38
It's probably been asked before, but I didn't find any other threads that fits my question good enough:
- I have a class GUI and a class Control.
- GUI is instanciated through Control, I will not make any new instances of Control or GUI.
- When a button is clicked in GUI, I want it to fire a method in Control.

I can think of 2 possible alternatives for doing this
A) In same way manage to have the actionPerformed-method in GUI to fire methods in Control. I tried searching for this, but I don't really know exactly what to search for, I've only found examples where everything happens in one class.
B) Make actionPerformed change a variable somewhere in GUI, and then use a timer or similar in Control to check regularly if the variable in GUI has changed. Haven't tried making any timers yet, but I suppose it should work, although it might not be the most effective way.

So, is it even possible to make actionPerformed fire a method in another class without instanciating it?
If not, is there any better way than using a timer to check a variable for changes?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
How about . . .

  • Passing a reference to Control to GUI,
  • and invoking methods on that?
  • Anybody else:

    Apart from the fact that you will have to compile both classes in the same compilation call (javac -d . GUI.java Control.java), can you see any potential problems with that approach, maybe threading problems?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38765
        
      23
    Originally posted by Kari Nordmann:
    So, is it even possible to make actionPerformed fire a method in another class without instanciating it?
    A static method, maybe, but not an instance method.
    Kari Nordmann
    Ranch Hand

    Joined: Feb 12, 2007
    Posts: 38
    Originally posted by Campbell Ritchie:
    [QB]How about . . .

  • Passing a reference to Control to GUI,
  • and invoking methods on that?
  • [CODE]public class Control
    {
    . . .
    GUI myGui = new GUI();
    myGUI.setControl(this);

    Amagad I didn't even know that was possible, that made it so much eeasier than I imagined Thanks alot
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38765
        
      23
    Suggested enhancement:

  • Put all the methods you want to call into a Control interface.
  • Change the title of the Control class.
  • Make the renamed Control class implement the Control interface.
  • Guido Sautter
    Ranch Hand

    Joined: Dec 22, 2004
    Posts: 142
    I'd pass the implementation of the Control interface to the GUI as a constructor argument, no need for a setter method here, since the reference is essential for the GUI to do work. Plus, changing the Control a GUI instance works on should be a rather rare action ...
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38765
        
      23
    I just wanted to be sure there would be no problems with escape of this references from the constructor. That's why I suggested the set method, even though it might be redundant.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Firing actionevents between classes