Solved

How to call functions on parent window

Posted on 1998-08-10
10
276 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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now