Solved

What this mean? embedded function in a constructor

Posted on 2002-03-27
4
311 Views
Last Modified: 2010-03-31
According to the code below:

//==========================================
public class BrowserPanel extends foo {

  private WebBuilder builder;
  private IlrBuilder.Ruleset ruleset = null;
  private IlrBuilder.Rule rule = null;
  private String packetName = null;

  public BrowserPanel(WebBuilder builder) {
    this.builder = builder;
    IlrWJavaAction ruleAction = new IlrWJavaAction()
    {
      public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
        project = (IlrBuilder.Project)args[0];
        IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
        getBuilder().setElement(rule);
        getBuilder().updateFrames(port);
      };
    }
    setSelectPacketAction(packetAction);

    ...
}
//======================

After the new IlrWJavaAction(), what mean the declaration of the embedded function perform()?

When this function can be called and what she's doing in the constructor?


Mario
0
Comment
Question by:mhebert
  • 2
4 Comments
 
LVL 2

Expert Comment

by:userquin
ID: 6899643
Hi:

I do not know anything about IlrWJavaAction class or interface but this example may be obvious:

This example illustrates an example about java.awt.event.ActionListener. As you know (or you must know) ActionListener is an interface to listen button actions. There is a method in button class called addActionListener(ActionListener e) that adds to this button a object that listen to actions performed in this object (button). When a button is clicked (for example), the button (itself) generates an event (ActionEvent) and it calls actionPerformed(ActionEvent e) method of all action listeners added to it self via addActionListener method. So you have a VIEW/CONTROLLER MODEL. Sometimes (when listener has only a method) this listeners are added to the view with anonymous classes so you do not have to create a class to this object. In this example you will have code that look like this:

JButton x = new JButton("...);
x.addActionListener(new ActionListerner() {
  public void actionPerformed(ActionEvent e) {
    ....
    ....
  }
});

When you compile this class, compiler creates an anonynous class called Myclass$1.class. In this case, actionPerformed method will be called when someone clicks the button.

Hope this help.

Bye.

J.
0
 

Expert Comment

by:0xDEADBEEF
ID: 6899681
The IlrWJavaAction object ruleAction in fact is an anonymous class derived from IlrWJavaAction. The declaration of the 'void perform' method specifies what to do if ruleAction.perform(bla) is called.

Anonymous classes are used for classes that are only used once in a very special context (such as event handling).
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6900536
> After the new IlrWJavaAction(), what mean the
> declaration of the embedded function perform()?

It's just defining an (anonymous) inner class.

> When this function can be called and what she's doing in
> the constructor?

The function can be called using:

ruleAction.perform(a, b, c);

In the BrowserPanel ctor she defines an inner class which is a subclass of IlrWJavaAction.
It is identical to the following:

public class BrowserPanel extends foo {

 private WebBuilder builder;
 private IlrBuilder.Ruleset ruleset = null;
 private IlrBuilder.Rule rule = null;
 private String packetName = null;

 public BrowserPanel(WebBuilder builder) {
   this.builder = builder;
   IlrWJavaAction ruleAction = new MyIlrWJavaAction()
   setSelectPacketAction(packetAction);

   ...
  }

  class MyIlrWJavaAction extends IlrWJavaAction
  {
   {
     public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
       project = (IlrBuilder.Project)args[0];
       IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
       getBuilder().setElement(rule);
       getBuilder().updateFrames(port);
     };
   }
  }
...

0
 
LVL 92

Expert Comment

by:objects
ID: 6903973
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
CSV file parsing thru Java 13 40
jmss example java 2 33
java imports not found 4 51
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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…

733 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