Results 1 to 3 of 3
  1. #1
    barts2108 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Cool Register a callback function in other class

    Hi All

    I am working on a program that contacts a PLC (programmable logic controller) using Java.

    I have one class (Class A) that contacts the PLC and is able to add listeners for items changed inside the PLC (for example Input/Output changes).

    Another class is the LED class (Class B). This graphically drawing a circle meant to show the status of an input or output.

    Now I want the Class B to invoke a 'registration function' in Class A passing which Input/Ouput to observe. Class A then must create a listener function for that Input/Output. When the Input/Output changes the listener function will be called (this is handled by the PLC manufacturer software). The listener for that IO then must invoke a method in the Class B so that I can paint the correct status

    The window handling, led painting (Class B) and PLC connection (Class A) I can all get to work. If I instantiate the LED from Class A it even works, however, I do not want the Class A to know anything about which items will be used.

    Thus if I want to show the IO status with a LED I use a LED Class (Class B) or if I prefer a BarGraph, I will create a Class C.

    Hope it is a bit clear what I want to establish. Almost all is working except that the LED class can register itself to de PLC Class to get a callback when the item changes.

    Any hints / code samples would be highly appreciated. I am experienced programmer (VB, C) but just started with Java.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    want the Class B to invoke a 'registration function' in Class A
    Look at any of Java's listeners for the template.
    Create an interface for the listener and have B implement it.
    In B call an add<your listener>(<your interface>) method in A passing a this reference.
    Have the add method save the reference. When A want to tell B something it calls the interface method as defined in the interface.

  3. #3
    barts2108 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Norm,

    Thanks for the quick answer. Going to work on this and will feedback the result.

Similar Threads

  1. Need help with get function
    By calicocal in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 07:59 PM
  2. Setting a timeinterval callback ???
    By wallys in forum New To Java
    Replies: 1
    Last Post: 08-19-2008, 05:52 PM
  3. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:33 PM
  4. can't register a MySQL driver
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-03-2008, 11:13 PM
  5. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •