Solved

How to call functions on parent window

Posted on 1998-08-10
10
306 Views
Last Modified: 2008-02-01

 Is there anyone who can tell me how to call function on the parent window?
 For the following example: doDelete() can not be accessed inside the deleteDialog.
 
 //payment.java
 public class payment extends Frame implements ActionListener
 {
     . . .  

  public void doDelete() throws SQLException
  {  
      query = "DELETE FROM customer WHERE ID ="+txtID.getText();          
      s = c.createStatement();
      s.execute(query);
      doNew(); //set all TextFields empty
      c.commit();
      s.close();                
  }

  public void actionPerformed(ActionEvent e)
  {
       if (e.getSource() == btnDelete)
       {
           if(!txtID.getText().equals(""))
           {                    
               deleteDialog dialog = new deleteDialog(this);
               dialog.show();
           }                                  
       }
  }
   
    . . .
 }

 //deleteDialog.java

 public class deleteDialog extends Dialog{
   public deleteDialog(Frame parent){
   super(parent, "Delete Record", true);
   . . .
   }
   public boolean handleEvent(Event event){  
       if(event.id==Event.ACTION_EVENT && event.target==yes)
       {
            doDelete();       //function on the parent window
            setVisible(false);
       }
       . . .
    }
 }
0
Comment
Question by:leeann
  • 5
  • 4
10 Comments
 

Expert Comment

by:awilkins
ID: 1232311
Have you tried
                     payment.doDelete();
instead of simply doDelete();?
0
 

Author Comment

by:leeann
ID: 1232312
If payment.doDelete() instead of doDelete(), got a compiler error message:
deleteDialog.java: Can't make static reference to method void doDelete() in
class payment
             payment.doDelete();
0
 
LVL 16

Expert Comment

by:imladris
ID: 1232313
Save the reference to the parent and use that:



 //deleteDialog.java

    public class deleteDialog extends Dialog{
      payment pp;
      public deleteDialog(Frame parent){
      super(parent, "Delete Record", true);
      pp=parent;
      . . .
      }
      public boolean handleEvent(Event event){  
          if(event.id==Event.ACTION_EVENT && event.target==yes)
          {
               pp.doDelete();       //function on the parent window
               setVisible(false);
          }
          . . .
       }
    }

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:leeann
ID: 1232314
Imladris,
Sorry! Here was the error message:
deleteDialog.java: Incompatible type for =. Explicit cast needed to convert
java.awt.Frame to payment.
                                  pp = parent;
0
 
LVL 16

Accepted Solution

by:
imladris earned 60 total points
ID: 1232315
Yes, of course. Try this: 


//deleteDialog.java

    public class deleteDialog extends Dialog{
      payment pp;
      public deleteDialog(Frame parent){
      super(parent, "Delete Record", true);
      pp=(payment)parent;
      . . .
      }
      public boolean handleEvent(Event event){   
          if(event.id==Event.ACTION_EVENT && event.target==yes)
          {
               pp.doDelete();       //function on the parent window
               setVisible(false);
          }
          . . .
       }
    }
0
 
LVL 16

Expert Comment

by:imladris
ID: 1232316
parent was declared as a Frame in the constructor. payment is a subclass of frame, and so, although it can be converted, Java will not do so automatically.

You can use a subclass of an object silently (for instance when you passed payment as a Frame) since you are promising less (payment has more stuff than Frame, so to pass it off as a Frame is safe; it will have all of Frames properties and more).

But you can't use a superclass of an object silently, since you would be promissing more (ditto, i.e. for a Frame to be a payment, it will need extra methods etc.). So to assign parent to pp, you must do an explicit cast, which allows the JVM to check whether parent is in fact a payment or not.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1232317
In retrospect that probably wasn't an incredibly clear explanation. Let me put it this way instead:

payment is a subclass of Frame. Therefore payment is a superset of Frame in terms of the methods and variables it provides.

So it is safe to use a payment instead of a Frame (since a payment can provide everything a Frame can).

However, you can't use a Frame instead of a payment, since a Frame won't have all the methods of a payment (like doDelete).
But if the Frame you have is referring to what is actually a payment, you can cast it to a payment (which allows the JVM to verify this), and then use it.

0
 

Author Comment

by:leeann
ID: 1232318
Hi! imladris,
Good news was no more compiler errors, but got error messages during running
time when clicked the button "Delete" on payment.

Exception occurred during event dispatching:
java.lang.NoSuchMethodError: deleteDialog:method <init><Ljava/lang/String;>V not
found.
       at payment.actionPerformed(payment.java:256)
       at java.awt.Button.processActionEvent(Button.java:248)
       at java.awt.Button.processEvent(Button.java:221)
       at java.awt.Component.dispatchEventImpl(Component.java:2041)
       at java.awt.Component.dispatchEvent(Component.java:195)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:167)
       at java.awt.EventDispatchThread.run(EventDispatchThread.java:45)


0
 
LVL 16

Expert Comment

by:imladris
ID: 1232319
It is apparently complaining about this line:               

deleteDialog dialog = new deleteDialog(this);

From the error message it seems to be looking for a constructor for the deleteDialog class (method <init>) that has a string argument (Ljava/lang/String). The line as you quoted seems fine, however java seems to think the argument is a string, as if you have declared a "String this" somewhere, or quoted it (like: deleteDialog("this")).

0
 

Author Comment

by:leeann
ID: 1232320
Imladris, thank you for your big help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question