Solved

How to call functions on parent window

Posted on 1998-08-10
10
286 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 132
Spring Framework HTTPSession management 1 24
web services creation SOAP vs REST 5 20
table example 4 24
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

895 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

14 Experts available now in Live!

Get 1:1 Help Now