Solved

How to call functions on parent window

Posted on 1998-08-10
10
292 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
diffSum example 4 36
iterator/ListIterator approach 17 25
Unhandled exception type Exception 18 28
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

813 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

12 Experts available now in Live!

Get 1:1 Help Now