java - Swing: How to achieve forwarding of all events from subcomponents to parent container? -
I seek to create a swipe component to find events in all your original containers for a simple way I am / strong> (or even all parents till the root). Edit : First, let me present your existing solution for this. This is a bit of a solution. Usage: By looking at the code, will you say that this is a good concept? Can you give better suggestions?
Where do I need it? I have a diagram editor. To move the ingredients to the main press and mouse clicks (immediately to set itself up as "active", when the user clicks on a sublet of that component).
Public Interface IUiAction {Zero Display (Component C); } Execute public static zeroCurrent UIIation (container parent, IUiAction verb) {if (parent == blank) {return; } For (component c: parent.getComponents ()) {if (c! = Null) {action.perform (c); }} For (component c: parent.getComponents ()) {if (the example of container) {performRecursiveUiAction (container), action); }}} / ** * 1) Add the listener to the container and all existing components (recursively). * 2) By adding a container listener to the container, make sure that all additional added * components will also receive the desired listener. * Useful example: Make sure that the whole component * Every component in the tree will respond to the mouse click. * / Public static void addPermanentListenerRecursively (Container Container, Final IUiAction Adder) {Final ContainerListener addingListener = New ContainerAdapter () {@Override Public Zero elementAdded (ContainerEvent e) {adder.perform (e.getChild ()); }}; // Step 1) Display the UIAction (Container, Adder); // Step 2) performRecursiveUiAction (Container, New IUiAction () {@Override Public Zero Display (Component C) {(C Container instanceof) {{(Container) c) .addContainerListener (addingListener)}}}}; }
addPermanentListenerRecursively (someContainer, New IUiAction (@Override Public Zero Display (component c) {c.addMouseListener (somePermanentMouseListener);}) );
The problem with my current concept is that it is only forwarding events, for which the listener was manually specified. / P>
It seems that it will be difficult to find if every component in your scene handles a single mouse event i.e. , If the user leaves item 1 , item 2 , will he also process those incidents? If I understand correctly, then you can move mouse actions on this
and this.parent
and this.parent.parent
Looking to handle . In that case the recycling will be up and down,
You can create an interface
like this:
Public Interface MyInterface () {Public Zero Exhibit Special Event (Event Event); }
Then you can apply this interface to your container. Your components will then need to include this call to deal with their appropriate event:
}
Comments
Post a Comment