5 Replies Latest reply: Dec 1, 2006 6:49 PM by 807607 RSS

    Calling my the parent class method from within the child

    807607
      i wrote a program that involves the main class instantiating muliple children. how can i get an event within the child class (say a button being pushed) within the child class to trigger a method in the parent class.

      if its important the child class creates its own JFrame making it basically a complicated popup dialog box thing.
        • 1. Re: Calling my the parent class method from within the child
          807607
          You are talking about a (parent) class that creates a bunch of other objects that then need to connect back. Yes?

          Message was edited by:
          tjacobs01
          • 2. Re: Calling my the parent class method from within the child
            807607
            i have the public class that is the main window i have the method in. it calls upon a different class (which creates its own JFrame, does its things and then closes said JFrame) then theres the buttonlistener class within that second class. i want to call upon the method in the primary class from within that button listener class
            • 3. Re: Calling my the parent class method from within the child
              807607
              Here's one solution:
              import javax.swing.*;
              import java.awt.event.ActionListener;
              import java.awt.event.ActionEvent;
              
                   class Test extends JFrame {
              
                        private JButton btn;
                        private Main buckFutter;
              
                        public Test(Main buckFutter) { 
              
                             this.buckFutter = buckFutter;
                             
                             btn = new JButton("Click Me!");
                             
              
                             getContentPane().add(btn);
                             pack();
                             setTitle("Child");
                             setDefaultCloseOperation(EXIT_ON_CLOSE);
                             setLocationRelativeTo(null);
              
                                  btn.addActionListener(new ActionListener() {
                                       public void actionPerformed(ActionEvent evt) {
                                            doEvent();
                                       }
                                  });
              
                        }
              
                        public void doEvent() { buckFutter.showMessage(); }
              
                   }
              
                   public class Main {
              
                        public Main() { new Test(this).setVisible(true); }
              
                        public static void main(String[] argv) { new Main(); }
              
                        public void showMessage() { System.out.println("Calling this from the Main Class."); }
              
                   }
              • 4. Re: Calling my the parent class method from within the child
                807607
                Want another solution? You can also declare those methods in your main class as static (which I recommend you read up on before using) to accomplish similar tasks:
                import javax.swing.*;
                import java.awt.event.ActionListener;
                import java.awt.event.ActionEvent;
                 
                     class Test extends JFrame {
                 
                          private JButton btn;
                           
                          public Test() { 
                 
                               btn = new JButton("Click Me!");
                               
                 
                               getContentPane().add(btn);
                               pack();
                               setTitle("Child");
                               setDefaultCloseOperation(EXIT_ON_CLOSE);
                               setLocationRelativeTo(null);
                 
                                    btn.addActionListener(new ActionListener() {
                                         public void actionPerformed(ActionEvent evt) {
                                              doEvent();
                                         }
                                    });
                 
                          }
                 
                          public void doEvent() { Main.showMessage(); }
                 
                     }
                 
                     public class Main {
                 
                          public Main() { new Test().setVisible(true); }
                 
                          public static void main(String[] argv) { new Main(); }
                 
                          public static void showMessage() { System.out.println("Calling this from the Main Class."); }
                 
                     }
                • 5. Re: Calling my the parent class method from within the child
                  807607
                  I seem to be not the only one who's surprised by the presence of java.beans.EventHandler which creates dynamic proxies for arbitrary event handlers ...