3 Replies Latest reply: Oct 10, 2012 2:54 AM by TPD-Opitz RSS

    How to access the Swing Conponents in different Classes

    Knidhi-Oracle
      Hi
      In swing based application, Parent Frame filled with multiple Panels, for each panel i created separate class and added the required swing component.
      components in one panel class require to update/modify components in another panel class.
      For solving this problem i made the components & required methods in a class as STATIC and directly access from another class.

      Is it right approach?
      is there any other approach to solve this issue?

      Thanks
      nidhi
        • 1. Re: How to access the Swing Conponents in different Classes
          Jörg
          One very common approach is to use so called setter and getter methods to access objects in a class:
          class Fruit {
            String type;
            int quantity;
          
            public String getType() {
              return type;
            }
          
            public void setQuantity(int i) {
             quantity= i;
            }
          
          }
          • 2. Re: How to access the Swing Conponents in different Classes
            morgalr
            Just pass a reference to the object you need to reference to the second object:
             MySecondObject o = new MySecondObject(myFirstObject);
            • 3. Re: How to access the Swing Conponents in different Classes
              TPD-Opitz
              knidhi wrote:
              Hi
              In swing based application, Parent Frame filled with multiple Panels, for each panel i created separate class and added the required swing component.
              components in one panel class require to update/modify components in another panel class.
              For solving this problem i made the components & required methods in a class as STATIC and directly access from another class.

              Is it right approach?
              No.
              is there any other approach to solve this issue?
              Yes. Learn about the MVC design pattern.

              You already implemented the V(iew)-part.
              Now you have to create a M(odel) to hold the Information you want to manipulate and display and a C(ontroller) that changes this information upon the user input (could be combined...)
              Usually the display components register themselves as Listeners to the model and the model publishes state changes to who ever has been registered...

              bye
              TPD