Results 1 to 3 of 3

Thread: Passing data in a program with GUI, HOW ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2002
    Posts
    12

    Question Passing data in a program with GUI, HOW ?

    I've written a java application consisting of three classes, two of which are coded to construct two different user interfaces for acquiring data from the user while the remaining one consists of the main() function and controls the flow of the entire program.However, due to the property of event-driven programming, I find it very hard to pass data from the two GUI back to the main class for further processing. And my question is, what is the best and efficient way of passing data between classes in a GUI program such as my one, and how ? Appreciate if a simple example could be given ! Thanx.


  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You could put getters and setters in your classes.
    Then retrieve the needed values via those methods.
    Code:
    class GUI{
      private String fname; 
      private String lname; 
    
      public void setfName(String fname){this.fname = fname;}
      public String getfName(){return fname;}
     
      public void setlName(String lname){this.lname = lname;}
      public String getlName(){return lname;}
     }
    
     public  class X{
      public static void main(String[] args){
        GUI gui = new GUI();
        gui.setfName("Bill");
        gui.setlName("Kasmeyer");
        System.out.println(gui.getfName());
        System.out.println(gui.getlName());
        }
    }

  3. #3
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    Another example is to use Observers and Observables, it's a powerful method of interprocess communication, if you have the GUIs as observables, and the main class as an observer for both GUI classes it will allow it to monitor changes, or actions that you specify. It also is probably a good way because of how the jvm handles the observable and observer processes.

























    Infinity isn't large it's just incomprehensible..........

Posting Permissions

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



Click Here to Expand Forum to Full Width