Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming Question

  1. Nov 30, 2004 #1
    Hi! I'm trying to build an object oriented system (in Java). There are several classes that provide the functionality of the system and GUI classes that use this functionality. The issue is how will the inner classes notify the GUI when events happen (like finishing reading from the disk, or a new message from the network), since they aren't aware of the existence of GUI classes and cannot call its methods. :confused:
  2. jcsd
  3. Nov 30, 2004 #2
    Java doesn't have polymorphism like C++, so it's tricky.

    Can't you implement a globally callable method within the GUI part that allows the lower level objects to pass messages to the user? Kind of like an alert box in javascript, or a Windows message box.

    It's not a perfect solution, I know, as it goes against the object oriented idea.
  4. Nov 30, 2004 #3
    Hi Ceptimus,

    I don't see the need for polymorphism except that there could be a general GUI class to which inner classes send messages.
    By the way polymorphism is supported in Java. A superclass reference can point to objects of any of its subclasses.

    I would need several such methods to make them global/static like a message box, flashing sign, drawing an image, etc. In this case the inner classes must have knowledge of the top level GUI class. On the other hand these classes can have a pointer/reference to an object of the GUI class to call its methods, but I don't consider this as good practice.
  5. Dec 1, 2004 #4
    I usually maintain a class wherein i keep a lot of flags (static) ...
    i update these flags as an when events occur ... all classes refer this "flag class" periodically and take actions accordingly. This is one way of message passing.

    -- AI
  6. Dec 1, 2004 #5
    No way. Keeping flags would require polling to check their values periodically. But this is costly because of more programming and more time overhead. I'd rather use a bidirectional association between the GUI class and the other classes. I just had thought that the inner classes could generate signals, maybe events or exceptions.
  7. Dec 2, 2004 #6
    I never said my method was fast, but yes definitely its the easiest.

    If ur application is speed demanding then there is a way out using swing's invokelater
    check this out,
    http://forum.java.sun.com/thread.jspa?tstart=15&forumID=424&threadID=566282&trange=15 [Broken]

    Hopefully this helps.

    -- AI
    Last edited by a moderator: May 1, 2017
  8. Dec 2, 2004 #7
    I'm not sure it is the easiest. Maybe if you're just planning on writing a one-off and never touching it again. But if you actually expect to maintain the code then your method will likely end up being a lot of work.

    You've basically just created a bunch of global variables and made every event method dependent on them - it sounds pretty fragile. Everything is interconnected, and race conditions could creep in really easily.

    You could always do something where GUI classes register themselves with the low level classes, and then when the low-level class does something eventworthy it calls a method on all of the classes registered with it. The low-level events would have to track what's registered with it, but it wouldn't really need to have any understanding of the GUI classes.
  9. Dec 2, 2004 #8
    Yes, event handling is the optimal scheme. It might have some time overhead, but it is clean design and is very good practice particularly for big systems.
  10. Dec 2, 2004 #9
    Well, it shouldn't have a huge time overhead. All you need (at least to start) is a standard handler interface and some simple event dispatching code.
  11. Dec 3, 2004 #10
    LOL!! You got it!! :biggrin:

    -- AI
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook