Results 1 to 7 of 7
  1. #1
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default How to Update data from another class to main class

    Hi !

    I have one main class, where is my gui code.

    I have make another class for serial port, where is my all methods of serial ports & serial events.

    In my main class i have 2 button for connect & disconnect serial communication.

    By checking boolean status Connect & Disconnect both are working by calling method from my another class.


    In my serial event i am receiving data every second. It is working i can look by printing on console.

    My problem is in my main class how can i know data is received in another class from serial port ?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to Update data from another class to main class

    As I recall, your serial port has an event handler associated with it. So have the other class implement the handler so it too can
    receive events.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to Update data from another class to main class

    Quote Originally Posted by Roshani View Post
    My problem is in my main class how can i know data is received in another class from serial port ?
    Does your other class know when data arrives from the serial port? If so, make your object from the main class known to the other class, so the other class can tell the main class when data has arrived.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default Re: How to Update data from another class to main class

    No Main class don't know when data received in another class.

    If so, make your object from the main class known to the other class, so the other class can tell the main class when data has arrived.
    I have created object like this in my main class

    Java Code:
    SerialPort sp = new SerialPort();
    Can Some one provide me with simple example.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to Update data from another class to main class

    If the code in SerialPort receives events from the GPS, then its code could call listeners passing that event. In the code that creates the SerialPort object add:
    Java Code:
      sp.addEventListener(this); //  tell SerialPort to call my event listener
    In the SerialPort class:
    Java Code:
      void addEventListener(MyEventListener mel) {
         this.mel = mel; //  save event listener
         // NOTE: This is very simple.  Real solution would need a list of mel objects
       }
    
    //  when an event happens, call the listener
       mel.processEvent(theEvent);  // call event listener and pass info about the event
    Last edited by Norm; 07-17-2016 at 04:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Roshani is offline Member
    Join Date
    Jul 2016
    Posts
    18
    Rep Power
    0

    Default Re: How to Update data from another class to main class

    Thanks for example trying to change according to you.

    As you know , i am novice, i am slow.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to Update data from another class to main class

    This thread of yours:

    http://www.java-forums.org/new-java/...tml#post412580

    shows your other class implementing a SerialPortEventListener. That is what Jos was asking. Any number of classes can listen
    for events from a single source.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 05:09 PM
  2. Replies: 16
    Last Post: 11-20-2013, 12:43 PM
  3. Error: Could not find or load main class Test.class
    By java_aim in forum New To Java
    Replies: 11
    Last Post: 08-13-2013, 05:05 PM
  4. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  5. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 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
  •