Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how do you broadcast an Event to an entire subtree?

aappddeevv12May 27 2013 — edited May 28 2013
I need to broadcast an event to an entire subtree. However, some code tests have not been able to shed light on how to do this.

Here's some code that illustrates the issue. I had expected that when you hit the button, the entire tree would get a event filter request but even after playing with all the parameters, sources and event targets, I cannot get an event to hit all the sub-children. Thoughts on how to get an event to broadcast to all the subtrees? I need the text field's to filter on all of the ximl events that pass its way.
public class TestJavaFxEventing2 extends Application {

	public static void main(String args[]) {
		Application.launch(TestJavaFxEventing2.class, args);
	}
	
	Scene s;
	VBox vbox;
	TextField tf, tf2;
	SplitPane sp;
	Button button;

	protected void sendEvent(ActionEvent event) {
		System.out.println("sending...");
		XimlEvent2 pkg = new XimlEvent2(button, null, XimlEvent2.ANY);
		pkg.setValue(Calendar.getInstance().getTime());
		// Pretend that the button fire this...
		button.fireEvent(pkg);
	}

	@Override
	public void start(Stage arg0) throws Exception {
		vbox = new VBox();
		s = new Scene(vbox, 500, 500);
		sp = new SplitPane();
		sp.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("sp filter ximl event");
			}
		});

		button = new Button();
		button.setText("Send Event");
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				sendEvent(arg0);
			}
		});
		button.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("button filter ximl event");
			}
		});
		button.addEventHandler(XimlEvent2.ANY,  new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("button handler ximl event");
			}
		});
		vbox.getChildren().addAll(button, sp);
		
		vbox.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			public void handle(XimlEvent2 arg0) {
				System.out.println("vbox filter ximl event");
			};
		});
		s.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("scene filter ximl event");
			}
		});
		
		tf = new TextField();
		tf.setText("tf");
		tf2 = new TextField();
		tf2.setText("tf2");
		sp.getItems().add(tf);
		sp.getItems().add(tf2);
		
		tf2.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("tf2 filter ximl event");
			}
		});
		tf2.addEventHandler(XimlEvent2.ANY,  new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("tf2 handler ximl event");
			}
		});
		
		tf.addEventFilter(XimlEvent2.ANY, new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("tf filter ximl event");
			}
		});
		tf.addEventHandler(XimlEvent2.ANY,  new EventHandler<XimlEvent2>() {
			@Override
			public void handle(XimlEvent2 arg0) {
				System.out.println("tf handler ximl event");
			}
		});

		arg0.setScene(s);
		arg0.show();
	}

	
	protected static class XimlEvent2 extends Event {
		
		public static final EventType<XimlEvent2> ANY =
	            new EventType<XimlEvent2> (Event.ANY, "XIML");
		
	    public static final EventType<XimlEvent2> DATACONTEXT =
	    		new EventType<XimlEvent2>(ANY, "DATACONTEXT");

	    public XimlEvent2(final EventType<? extends XimlEvent> eventType) { 
			super(eventType);
		}
			
		public XimlEvent2(Object source, EventTarget target, final EventType<? extends XimlEvent2> eventType) { 
			super(source, target, eventType);
		}

		Object value;

		public Object getValue() {
			return value;
		}

		public void setValue(Object value) {
			this.value = value;
		}	
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 25 2013
Added on May 27 2013
2 comments
234 views