java - Swing: How to achieve forwarding of all events from subcomponents to parent container? -


"itemprop =" text ">

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 :
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).

First, let me present your existing solution for this. This is a bit of a solution.

  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)}}}}; }  

Usage:

  addPermanentListenerRecursively (someContainer, New IUiAction (@Override Public Zero Display (component c) {c.addMouseListener (somePermanentMouseListener);}) );  

By looking at the code, will you say that this is a good concept?
The problem with my current concept is that it is only forwarding events, for which the listener was manually specified. / P>

Can you give better suggestions?

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:

public static void performRecursiveUiAction (component computer application, event event) {if (comp.getParent () = = Null) {return; } If (Comp.JPint () Instant of My Interface) ((MyInterface) Comp. JetPant ()). Display special events (events); } This utility. PerformersWeiAction (Comp.JPper (), Event);

}


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -