开发者

Java Events - How to send extra information to it?

I am a C# dev but currently working on Java. In C# the events have two arguments: the eventArgs - information about the event iself - and the sender, which is information about the object who fired the开发者_JS百科 event.

Java has a slightly different approach, each event handler is a class (nested or not). I decided for creating no-nested classes because of code cleanness. What happens is that I am having problems because I can't access some information about the object that fired this event nor access to some elements of the class (other objects that may be useful for calculation).

How can I address these 2 issues?

Thanks, Oscar


An event in Java is not a special language feature and so you're free to design your event classes as you like - as long as you do not have to use given event classes from a certain framework or library.

If you want to have an event that knows it's creator, you may code it like this:

public class Event<T> {
  private T sender;
  private String message;
  public Event(T sender, String message) {
    this.sender = sender;
    this.message = message;
  }
  public T getSender(){return sender;}
  public String getMessage(){return message;}
}

and fire it like this:

private void fire(String message) {
  for(Listener listener:listeners) {
    listener.notify(new Event<Sender>(this, message));
  }
}

(assuming, this is a instance of the fictitious Sender class and you have a collection with Listeners for your events)


A JButton sends an ActionEvent to all registered ActionListeners. The constructor of this event takes the sender, which is the button object that caused the event. It provides a getSource() method to get it from the event, so

if (event.getSource() instanceof JButton) {
   JButton sender = (JButton) event.getSource();
}

will actually give you the button. The button again knows its "container", usually a JPanel. getParent() is the method to actually get it from the button (or any other component).

Another trick, if you want to know the dialog that holds a button, is to extend JButton and add a custom parentWindow field:

public class MyJButton extends JButton {
  private Window parentWindow = null;

  public MyJButton(Window parentWindow, String text) {
    super(text); // call the JButton constructor!!
    this.parentWindow = parentWindow;
  }
  // add all other constructors

  public getParentWindow() {return parent.Window;}
}

Use this class for your buttons and change the snippet from above to:

if (event.getSource() instanceof MyJButton) {
   Window window = ((MyJButton) event.getSource()).getParentWindow();
}


Your second issue is resolved either by going back to nested classes (unlike C#, nested classes in Java have access to the fields of the outer class, including non-static and private fields) or by passing an appropriate reference to the constructor of an event handler (if it's a single object that you want to have access to when processing an event).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜