[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to call functions on parent window

Posted on 1998-08-10
10
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 240 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

Hire Technology Freelancers with Gigs

Work with 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…
Suggested Courses

649 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