Results 1 to 2 of 2
  1. #1
    george_a is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default how to pass an arraylist from an object back to the parent object that it was created

    hi,
    I have a gui(GUI1) that creates a new object of another gui(GUI2), GUI2 is responsible for reading files and filter data and then i need those data to be sent back to GUI1.

    Which way is the best? Do I have to create an object on top of the 2 ? Is there some sort of listener that can be triggered by GUI2 to force GUI1 get the data from GUI2, since it has its reference? Any other way ?

    thanks,
    George

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I assume some event in GUI2 is supposed to close/hide GUI2 resources and pass control to GUI1. In a sense, you want GUI1 to listen to GUI2. That is a valid paradigm, but you would have to create your own listener apparatus.

    A simple way to accomplish the same thing, without the full flexibility, is to pass a reference to GUI1 when you construct GUI2. When the particular event occurs in GUI2, it deals with its resources, and then it calls a method in GUI1. GUI1 updates is components and makes sure they are visible.

    This is not an ideal design pattern, because GUI1 and GUI2 are now "tightly coupled".

    The following is an apparatus I created to implement generic events. Feel free to use it, but make sure you understand it first.

    Java Code:
    package utility;
    
    import java.util.Date;
    import java.util.EventObject;
    
    /**
     * This class defines a generic event used as part of the SimpleEvent framework. The goal of the
     * framework is to allow any class to dispatch events containing class-defined fields with minimal
     * coding.
     * <p>
     * The framework achieves this by allowing a class to instantiate one or more SimpleEventDispatcher.
     * SimpleEventDispatcher handles registration of listeners and event dispatching.
     * SimpleEventDispatcher always dispatches SimpleEvent instances. The class simply need implement
     * addFooListener() and removeFooListener() methods for each SimpleEventDispatcher it instantiates.
     * These methods simply invoke the corresponding method in SimpleEventDispatcher.
     * <p>
     * The framework gains flexibility through the use of generics and the design of SimpleEvent.
     * SimpleEvent must be constructed by passing an object of type ResultType. ResultType may be any
     * type; String for simple implementations or a custom object with multiple fields for more complex
     * ones. The custom object need only provide a constructor with parameters for each of its
     * properties and getters. SimpleEvent then provides a getResult<ResultType>() method, allowing the
     * listening class to access the event information.
     * 
     * @param <ResultType>
     *          the type returned by the event's getResult() method.
     */
    public final class SimpleEvent<ResultType>
        extends
        EventObject {
    	/** For serialization */
    	private static final long serialVersionUID = 7889694531295423118L;
    	/** The Date the event was created */
    	private final Date iDateCreated = new Date();
    	/** The result of the process */
    	private final ResultType iResult;
    
    	/**
    	 * Constructor.
    	 * 
    	 * @param pSource
    	 * @param pResult
    	 */
    	public SimpleEvent(final Object pSource,
    	    final ResultType pResult) {
    		super(pSource);
    		iResult = pResult;
    	}
    
    	/**
    	 * Get the date this event was created
    	 * 
    	 * @return the date created
    	 */
    	public final Date getDateCreated() {
    		return iDateCreated;
    	}
    
    	/**
    	 * Get the result from the process.
    	 * 
    	 * @return the result object
    	 */
    	public final ResultType getResult() {
    		return iResult;
    	}
    }
    Java Code:
    package utility;
    
    import java.util.ArrayList;
    
    /**
     * This class dispatches SimpleEvent's to registered listeners. The main intention of this class is
     * to allow contained classes to send information to the container without having to know anything
     * about the container. It is also usesful for worker threads to send information back to the
     * invoker. Synchronization is required.
     * <p>
     * As this class is inherently generic, it uses strong typing to ensure
     * listeners register themselves to the correct dispatcher.<br>
     * Classes instantiating this class should specify SimpleEventDispatcher<ResultType>.
     * <p>
     * The instantiating class should provide a method<br>
     * public final addFooListener(final SimpleListener<ResultType> pListener) { ....
     * fooDispatcher.addListnener(pListener);<br>
     * .. }<br>
     * to allow listeners to register.
     * 
     * @param <ResultType>
     */
    public class SimpleEventDispatcher<ResultType> {
    	/** List of iListeners */
    	private final ArrayList<SimpleListener<ResultType>> iListeners = new ArrayList<SimpleListener<ResultType>>();
    	/** List synchronization object, to avoid modification while iterating errors */
    	private final Object iListenersSync = new Object();
    
    	/**
    	 * Add a listener
    	 * 
    	 * @param pListener
    	 */
    	public void addListener(final SimpleListener<ResultType> pListener) {
    		synchronized (iListenersSync) {
    			iListeners.add(pListener);
    		}
    	}
    
    	/**
    	 * Fire a SimpleEvent
    	 * 
    	 * @param pSource
    	 * @param pResult
    	 */
    	public void fireSimpleEvent(final Object pSource,
    	    final ResultType pResult) {
    		final SimpleEvent<ResultType> simpleEvent = new SimpleEvent<ResultType>(pSource,
    		    pResult);
    		synchronized (iListenersSync) {
    			for (final SimpleListener<ResultType> efListener : iListeners) {
    				try {
    					efListener.simpleEventDispatched(simpleEvent);
    				}
    				catch (final Throwable t) {
    					ExceptionUtil.printStackTrace(t,
    					    "Invoking listener resultGenerated(pe)");
    				}
    			}
    		}
    	}
    
    	/**
    	 * Remove all iListeners
    	 */
    	public void removeAllListeners() {
    		synchronized (iListenersSync) {
    			iListeners.clear();
    		}
    	}
    
    	/**
    	 * Remove a listener
    	 * 
    	 * @param pListener
    	 */
    	public void removeListener(final SimpleListener<ResultType> pListener) {
    		synchronized (iListenersSync) {
    			iListeners.remove(pListener);
    		}
    	}
    }
    Java Code:
    package utility;
    
    import java.util.EventListener;
    
    /**
     * Listener interface for SimpleEvents.
     * <p>
     * The listening object invoke the appropriate add listener method on the dispatching object:
     * 
     * <pre>
     * source.addFooListener(new SimpleListener<ResultType>() {
     *   simpleEventFired(SimpleEvent<ResultType> pEvent) {
     *     // handler code
     *   }
     * });
     * </pre>
     * See the SimpleEvent documentation for information on how the event uses ResultType to allow
     * specific information to be returned.
     * 
     * @see SimpleEvent
     * @param <ResultType>
     */
    public interface SimpleListener<ResultType>
        extends
        EventListener {
    	/**
    	 * A process dispatched an event.
    	 * 
    	 * @param pEvent
    	 */
    	public void simpleEventDispatched(final SimpleEvent<ResultType> pEvent);
    }

Similar Threads

  1. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  2. Object ArrayList - increment solution needed badly!!
    By rugbyGeek in forum New To Java
    Replies: 4
    Last Post: 03-08-2008, 01:47 AM
  3. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 07:25 PM
  4. how to return an object from an arraylist
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •