• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Firing actionevents between classes

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
  •  
    Ranch Hand
    Posts: 142
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic