6 Replies Latest reply: Jun 29, 2011 4:18 AM by mKorbel RSS

    How does a jframe communicate with other classes???

    871981
      Hello people, sorry for my bad english and for my question that can be very dumb. But how does the frame communicate with other casses?
      Ok, my situation is the follow, i have a main class that contains :
      1- a array of class 'employees' and
      2- two 'frames' ( 'startscreen' and 'addemployee' screen)

      My intention is:
      1- in main class run the main frame ( asking if u want to add employees) then
      2- in addemployee, to grab data from the textfield of the created frame to create personalized employees and add them to the array of the main class when someone press the jbutton.

      My doubt is how will be the communication.
      What must i do?
      Must i create the second frame inside the first one or in the main class? But if i do this, how can pass it as an parameter?

      Thank you very much.
        • 1. Re: How does a jframe communicate with other classes???
          gimbal2
          If you need a frame to "communicate" with another frame, you always have the option of passing it along in the constructor.

          Note that a modal dialog seems to be more fitting for an add employee function in stead of a frame. The application frame would 'spawn' the addemployee dialog, in which case it can be like this:
          public class AppFrame extends JFrame {
            ...
          
            public void showAddEmployeeDialog(){
              AddEmployeeDialog dialog = new AddEmployeeDialog(this);
              dialog.createAndShow();
            }
          
            public void addEmployee(Employee emp){
               ...
            }
          }
          
          public class AddEmployeeDialog extends JDialog {
          
            private AppFrame app;
          
            public AddEmployeeDialog(AppFrame frame){
              this.app = frame;
            }
          
            ...
          
            public void pushedCreatedButton(){
              Employee emp = new Employee(...);
          
              app.addEmployee(emp);
            }
          }
          Very crude setup and typed from memory, so beware typos.
          • 2. Re: How does a jframe communicate with other classes???
            mKorbel
            @gimbal2

            that isn't good idea create lots of JDialogs on RunTime,

            create only one/two and reuse that, by removing all JComponents

            because any of Top-Layout Containers never will be GC'ed, after GC just returns value used for 2D Graphics

            rest of that is correct advice +1
            • 3. Re: How does a jframe communicate with other classes???
              DrClap
              But you would have to produce a large number of JDialogs to cause a significant memory leak, and if your application is producing that many JDialogs it's going to be extremely annoying for the user anyway.
              • 4. Re: How does a jframe communicate with other classes???
                DarrylBurke
                Cross posted
                http://www.java-forums.org/awt-swing/45842-how-does-jframe-communicate-other-classes.html

                db
                • 5. Re: How does a jframe communicate with other classes???
                  gimbal2
                  mKorbel wrote:
                  @gimbal2

                  that isn't good idea create lots of JDialogs on RunTime,

                  create only one/two and reuse that, by removing all JComponents

                  because any of Top-Layout Containers never will be GC'ed, after GC just returns value used for 2D Graphics
                  I will call that utter bull until you can provide evidence to support your claims. I'm pretty sure that evidence will relate to some old crippled version of Swing.
                  • 6. Re: How does a jframe communicate with other classes???
                    mKorbel
                    @gimbal2

                    :-) just joke http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime


                    if you means Java 1.6.025, then yes, you are right that's outdated :-), because there exist Java 1.6.026 update, but beware of for production code, because there are untested news in 26Update about Serializable and...

                    hint Top-layout Containers missed finalize(), sure except JFrame :-) otherwise wouldn't be possible close App based on Swing

                    anyway that's really possible in Java upTo 1.4.xxx, but I never tried that, and why, no reason(s) for Java after end-of-life ...