Results 1 to 2 of 2
  1. #1
    beezerbutt is offline Member
    Join Date
    Feb 2009
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    Talking Swing design patterns

    Hi

    As you know Swing relies on the gui thread handling requests for access to components as most (not all) are not thread safe.

    Rather than code something piecemeal, i googled Swing patterns and i got nothing. I looked on OReilly and the same.

    I then used a command dispatcher (Command and CommandFactory) pattern to implement my own way of handling interaction between what i call the "worker objects" and the GUI component of my application.

    However, i still would like to know the broader picture of patterns when it comes to Swing and Java GUI applications in general in order not to have to
    reinvent the wheel.
    Does anyone know of any?

    Obviously the MVC implementation of Swing brings to mind a whole host of questions with regard to patterns:

    1) Event processing: View and/or Model
    2) Model / Controller separation and how best to achieve this

    I ask all these questions for one simple reason.
    As my GUI application gets more and more complicated, i would rather have things in order now rather than have to refactor.

    Thanks in advance.

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

    Default

    Congratulations on taking an active interest in proper design! I'm passing on just a couple of things that will help you.

    First, do a general search on MVC (Model-View-Controller). That is useful for *all* GUI design. Even ASP.NET is finally adopting it (because of Ruby, although JSP developers have been using it forever.)

    Second, look at the Sun Tutorial on Concurrency. I added a brief summary and some code. Don't be surprised if it's broken ;-)

    The GUI thread is called the Event Dispatcher Thread (EDT).

    The class that governs the activities running on the EDT is EventQueue.

    To cause a method to run on the EDT, use the following. Note that the example is the proper way to initially display the GUI.
    Java Code:
    java.awt.EventQueue.invokeLater(new Runnable() {
    	public void run() {
    		new SimpleInputForm().setVisible(true);
    	}
    });
    Here is a helpful utility to list exceptions that occur on the EDT. Normally, the EDT just traps them and goes on. Don't be fooled when nothing actually breaks. Call setExceptionHandler before any Swing code.
    Java Code:
    package utility.swing;
    
    import java.awt.AWTEvent;
    import java.awt.Component;
    import java.awt.EventQueue;
    
    import utility.ExceptionUtil;
    
    /**
     * Handle exceptions on the event dispatch thread
     */
    public class AWTExceptionHandler {
    	/**
    	 * Call this before starting the EDT
    	 */
    	public static void setExceptionHandler() {
    		System.setProperty("sun.awt.exception.handler",
    		    AWTExceptionHandler.class.getName());
    	}
    
    	/**
    	 * Handle exceptions on the event dispatch queue
    	 * 
    	 * @param t
    	 */
    	public void handle(Throwable t) {
    		final AWTEvent event = EventQueue.getCurrentEvent();
    		final String eventDescription;
    		final Component source;
    		if (event != null) {
    			source = (Component) event.getSource();
    			eventDescription = "Source: "
    			    + source.getClass().getSimpleName()
    			    + " Event class: "
    			    + event.getClass().getSimpleName();
    		}
    		else {
    			eventDescription = "None available.";
    		}
    		ExceptionUtil.printStackTrace(t,
    		    eventDescription);
    	}
    }
    And two utilities, one used by AWTExceptionHandler, the second by the first.
    Java Code:
    package utility;
    
    public class ExceptionUtil {
    	/**
    	 * Entry point, for testing only
    	 * 
    	 * @param args
    	 */
    	public static void main(final String[] args) {
    		printStackTrace(new Throwable("This is a test."),
    		      "Testing.");
    	}
    
    	/**
    	 * Print a stack trace, only
    	 * 
    	 * @param t
    	 */
    	public static void printStackTrace(final Throwable t) {
    		printStackTrace(t,
    		      "");
    	}
    
    	/**
    	 * Print a stack trace, along with a message
    	 * 
    	 * @param t
    	 * @param message
    	 */
    	public static void printStackTrace(final Throwable t,
    	      final String message) {
    		String m = message;
    		if (t == null) {
    			return;
    		}
    		if (m == null) {
    			m = "";
    		}
    		System.err.println(DateUtil.formatCurrentDate()
    		      + ": "
    		      + m);
    		t.printStackTrace();
    	}
    }
    Java Code:
    package utility;
    
    import java.util.Date;
    import java.util.Formatter;
    
    /**
     *  
     */
    public class DateUtil {
    	/** Last millis returned by uniqueCurrentMillis() */
    	private static long sLastUniqueMillis;
    	/** Sync object for uniqueCurrentMillis() */
    	private static final Object sSyncObject = new Object();
    
    	/**
    	 * Format the current date, down to the millisecond
    	 * 
    	 * @return the formatted current date
    	 */
    	public static String formatCurrentDate() {
    		return formatDate(new Date());
    	}
    
    	/**
    	 * Format the date provided, down to the millisecond
    	 * 
    	 * @param pDate
    	 * @return the formatted date
    	 */
    	public static String formatDate(final Date pDate) {
    		final Formatter formatter = new Formatter();
    		String dateTimeMillis = null;
    		formatter.format("%1$tY-%1$tm-%1$td_%1$tH.%1$tM.%1$tS.%1$tL",
    		    pDate);
    		dateTimeMillis = formatter.toString();
    		return dateTimeMillis;
    	}
    
    	/**
    	 * Entry point, for testing
    	 * 
    	 * @param args
    	 */
    	public static void main(final String[] args) {
    		System.out.println(formatCurrentDate());
    	}
    }

Similar Threads

  1. Java Mashup API - Design Patterns Questionnaire
    By goldeneyeone in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-10-2009, 11:03 PM
  2. Replies: 2
    Last Post: 07-04-2007, 07:58 AM
  3. Design patterns
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 07:21 AM

Posting Permissions

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