This discussion is archived
2 Replies Latest reply: Jan 21, 2013 11:45 AM by 903019 RSS

Custom Event on FX components

903019 Newbie
Currently Being Moderated
Hi,

I have presenter which keeps my GUI components intact. I want to pass notifications to all components from a central place.
I created custom event and tried to pass that event on the root node thinking that javafx events goes from top to down thus all components will get that notification but its not working
at all so please help out.

My Custom Event
public class NotificationEvent extends Event {

  public static final EventType<NotificationEvent> NOTIFICATION_ARRIVED = new EventType(ANY, "NOTIFICATION_ARRIVED");
  private Notification notif;

  public NotificationEvent(Notification notif) {
    this(NOTIFICATION_ARRIVED);
    this.notif = notif;
  }

  public NotificationEvent(EventType<? extends Event> eventtype) {
    super(eventtype);
  }

  public NotificationEvent(Object obj, EventTarget target, EventType<? extends Event> type) {
    super(obj, target, type);
  }

  public Notification getNotification() {
    return notif;
  }
}
Place from where I fire custom event to the root node
   registerHandler(eventBus.addHandler(UserPermissionsEvent.TYPE, new UserPermissionsEventHandler() {
      @Override
      public void onUserPermissionEvent(UserPermissionsEvent event) {
         NotificationEvent notifEvent = new NotificationEvent(event.getNotification());
        getRoot().fireEvent(notifEvent);
      }
    }));
I put event handler on one of the button to see whether event travels to all components or not but it didn't.
logOutBtn.addEventHandler(NotificationEvent.NOTIFICATION_ARRIVED, new EventHandler<NotificationEvent>() {
      @Override
      public void handle(NotificationEvent event) {
        if (event instanceof NotificationEvent) {
          log.debug("----------------------Notification event on logout button -----------" + event.getNotification().getType());
        }
      }
    });
Am I missing something in this. Some input will be really helpful.

Thanks & Reagards,
Ash
  • 1. Re: Custom Event on FX components
    jsmith Guru
    Currently Being Moderated
    I'm interested in a good solution to this too.

    There is a nice tutorial on how to handle existing events => http://docs.oracle.com/javafx/2/events/jfxpub-events.htm
    But there is no corresponding tutorial on how to define and use your own custom events.
  • 2. Re: Custom Event on FX components
    903019 Newbie
    Currently Being Moderated
    Hi,

    I figure out something to make my custom events working. I don't know whether its a correct way or not.
    Still need some advice for fixing some issues.

    I created a EventDispatchChain for my presenter and appended all my components to that EventDispatchChain.
    So when I fire my custom event on the dispatch chain, it passes the event to all the components. I also have
    tree with treeItems in my application, for adding treeItem to the dispatch chain I used following code
      treeItem.buildEventDispatchChain(eventChain); 
    Problem is that I have too many items on the tree which I am adding to the event chain so when I fire event on the
    dispatch chain it throws below error
    java.lang.RuntimeException: java.lang.StackOverflowError
         at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:251)
         at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
         at com.sun.glass.ui.View.notifyMouse(View.java:922)
         at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
         at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
         at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:67)
         at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.StackOverflowError
         at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:195)
         at com.sun.javafx.event.EventHandlerManager.dispatchCapturingEvent(EventHandlerManager.java:159)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:31)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
    
     
    So suggest what is the best way to handle this issue because I need to attach events on each tree item so that each cell
    know what to do for incoming notification.


    Thanks & Regards,
    Ash

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points