Swinging from the Outside A guide to navigating Swing from the outside of Sun Brian Mason, Dir Software of Engineering, Teseda S295599
Space is big, really big. You might think it is a long way down to the chemist's, but that is peanuts to space. Swing toolkit, however, isn't that big, but it is still pretty big. Starting out it can be confusing. Understanding common issues and finding solutions is a key to successfully navigating Swing toolkit. By the end of this presentation, you will have an idea of the issues and a direction to start creating cool Swing Applications 2008 JavaOne SM Conference java.sun.com/javaone 2
A quick search of some Swing toolkit helpers Swing Java 2D JOGL Java 3D JSR-295 SwingX Java FX SWT JDIC JChart JSR-295 JBuilder JGoodies JXLayers Netbeans InteliJ ILOG Eclipse Timing Framework 2008 JavaOne SM Conference java.sun.com/javaone 3
Why Would a New Swing toolkit Developer be Confused? 2008 JavaOne SM Conference java.sun.com/javaone 4
Agenda Classification of Swing Applications Application Frameworks Threading and Task Management Making the User Wait Making it pretty Q&A 2008 JavaOne SM Conference java.sun.com/javaone 5
Classification of Swing Applications Purpose of classification is to understand what is important Focus discussion Recognize not all applications need the same things Classification is basically understanding the user Many other parameters go into user understanding besides target use Two broad classifications of Swing Apps are: Consumer Applications Business Applications 2008 JavaOne SM Conference java.sun.com/javaone 6
Business Applications Maybe Mission Critical Often set UI guidelines, Used All day Skinning not needed (or desired) Use of Animation and colors often controlled Needs to be very stable UI Must minimize eye strain I18N always a good idea, but localization often not needed Apps often not run as admin Example, OC UI Guidelines 2008 JavaOne SM Conference java.sun.com/javaone 7
Consumer Applications Less formal UI More Glitz allowed/expected Maybe used for shorter periods of time Varied user base. I18N and L10N very important Personalization such as Skins, important Often app is run as Admin on windows 2008 JavaOne SM Conference java.sun.com/javaone 8
Agenda Classification of Swing Applications Application Frameworks Threading and Task Management Making the User Wait Making it pretty Q&A 2008 JavaOne SM Conference java.sun.com/javaone 9
Application Framework Definition from Wikipedia: A software framework is a re-usable design for a software system (or subsystem) A Guideline/best practices and set of helper classes to build an application We are not talking about frameworks for: Animation UI Handling RDBMS Access An application framework is for the whole application, not small, yet important parts. Component Libraries are not frameworks 2008 JavaOne SM Conference java.sun.com/javaone 10
Qualities of Good Application Frameworks SIMPLE, SIMPLE and SIMPLE Is not intrusive, can be used or not (At the same time) Helps With Resource Management Thread Management Life Cycle Has a Standard Look and Feel? Same on all platforms? Matches Platform? Allows for Branding / Matching Corp UI standards If it is just as hard to develop the app with the framework as without, then it hasn't saved you any work (J. Murphy) 2008 JavaOne SM Conference java.sun.com/javaone 11
Example of Bad A framework requires all Windows derived from a base class Does everything a Special Way Does not allow components to move between applications Resources are stored centrally Functionality is tied to a named class Think MFC 2008 JavaOne SM Conference java.sun.com/javaone 12
Example Application Frameworks Great Frameworks we will not discuss The Brian Framework The John Framework The great unified Framework Cool looking Frameworks Swing Application Framework (Java Specification Request 296) Jmatter Starter Code from IDE 2008 JavaOne SM Conference java.sun.com/javaone 13
The Swing Framework (JSR-296), How does it line up? Does it meet the test Is it Simple? Does it stay out of your way? Helps With Resource Management Thread Management Life Cycle Has a Standard Look and Feel? Hold the thought. Lets Look at the Demo!!! 2008 JavaOne SM Conference java.sun.com/javaone 14
Simple JSR-296 Application 2008 JavaOne SM Conference java.sun.com/javaone 15
Advice for all Frameworks, libraries,blogs and books Double Check Everything Learn Basic Swing Toolkit Rules Do not assume things are correct If code uses threads, double check it again Check the code into Source Control Check Threading! Threading errors in book code Use Static Analysis for Threading. 2008 JavaOne SM Conference java.sun.com/javaone 16
Agenda Classification of Swing Applications Application Frameworks Threading and Task Management Making the User Wait Making it pretty Q&A 2008 JavaOne SM Conference java.sun.com/javaone 17
Embrace the Horror You cannot avoid multi-threaded Swing tookit Event Dispatch Thread (EDT) Do not block the EDT; EVER File System Access Network Access DB Access Even long memory based task, like sorting Swing Calls, unless specifically document otherwise, must be ON the EDT 2008 JavaOne SM Conference java.sun.com/javaone 18
Bad Threading Example (EDT Handler) Jlist filelist=new JList(); File dir=new File("."); File[] list=dir.listfiles(); DefaultListModel model=new DefaultListModel(); for(file f:list){ model.addelement(f.getname()); } filelist.setmodel(model); 2008 JavaOne SM Conference java.sun.com/javaone 19
Perfuming the hog (Threaded Handler) Runnable run = new Runnable() { public void run() { File dir = new File("."); File[] list = dir.listfiles(); DefaultListModel model = new DefaultListModel(); for (File f : list) { model.addelement(f.getname()); } filelist.setmodel(model);} }; Thread t=new Thread(run); t.start(); 2008 JavaOne SM Conference java.sun.com/javaone 20
Legal Thread Solution Runnable run = new Runnable() { public void run() { File dir = new File("."); File[] list = dir.listfiles(); DefaultListModel model = new DefaultListModel(); for (File f : list) { model.addelement(f.getname()); } loadthemodelsafely(model)} }; ExecutorService background=somefactory.getbackgroundexec() background.submit(run); 2008 JavaOne SM Conference java.sun.com/javaone 21
Legal Thread Solution Slide 2 void loadthemodelsafely(final ListModel model){ Runnable run=new Runnable(){ public void run(){ list.setmodel(model); } } } if(swingutilities.iseventdispatchthread()){ run.run(); }else{ //try catch block ommitted for readability SwingUtilities.invokeAndWait(run); } 2008 JavaOne SM Conference java.sun.com/javaone 22
Actions Meets Swing Worker A humble example of cleaner solution for blocking task Like Swing Worker but ties to actions Uses Swing Utils and threading behind the scene Has three abstract methods: preuiwork backgroundwork postuiwork 2008 JavaOne SM Conference java.sun.com/javaone 23
Threaded Actions Example class BlockingButtonAction extends BlockingAction{ public void preuiwork(actionevent e) { parent.setglasspane(gp); gp.setvisible(true);} public Object backgroundwork(actionevent e){ Thread.sleep(20000); return null;} public void postuiwork(actionevent e,object resultsfrombackground, Throwable error) { gp.setvisible(false); } 2008 JavaOne SM Conference java.sun.com/javaone 24
Threaded Actions 2008 JavaOne SM Conference java.sun.com/javaone 25
JSR-296 Actions Actions defined by @Action annotation on methods Must Bind Action to Components Handles Blocking and non-blocking actions Reporting method built in 2008 JavaOne SM Conference java.sun.com/javaone 26
JSR-296 Actions 2008 JavaOne SM Conference java.sun.com/javaone 27
Agenda Classification of Swing Applications Application Frameworks Threading and Task Management Making the User Wait Making it pretty Q&A 2008 JavaOne SM Conference java.sun.com/javaone 28
Blocking The User It's OK, they need a coffee break Why is it so hard? Keyboard Mouse Letting the user know what's happening When and how should we block the User? Never Only With a modal Dialog With a Glass Pane How about just a wait cursor???? You can never truly block a user Give them a Cancel Option so they use Your Solution There is a close widget on the window There is always kill -9 2008 JavaOne SM Conference java.sun.com/javaone 29
Blocking Solutions 2008 JavaOne SM Conference java.sun.com/javaone 30
Agenda Classification of Swing Applications Application Frameworks Threading and Task Management Making the User Wait Making it pretty Q&A 2008 JavaOne SM Conference java.sun.com/javaone 31
Making it Pretty Pretty does not always mean fancy Beauty is in the Eye of the Beholder Business apps used all day need to be Easy On the Eyes Remember guidelines for business apps Embrace alternatives Embrace Java 2D API Swing Labs Third Party Libs 2008 JavaOne SM Conference java.sun.com/javaone 32
Java 2D API Swing is built on Java 2D API Paint methods take a Graphics2D as parameter The docs will show Graphics, cast it Rich set of methods for 2D work Well done and easy to use Can really speed up performance 2008 JavaOne SM Conference java.sun.com/javaone 33
Swing Labs http://www.swinglabs.org Collection of several libraries SwingX Timing Framework JDesktop Integration Components (JDIC) SwingX-WS and more... Code is lab code It does change Put your copy in Source Control 2008 JavaOne SM Conference java.sun.com/javaone 34
GUI Builders Should we use GUI Builders? Arguments Against Ugly Code Doesn't support what I need Arguments For Speed Not everyone is the UI god you (think you) are Prettier Code Some Examples Netbeans software Intelli-J Jbuilder 2008 JavaOne SM Conference java.sun.com/javaone 35
GUI Builder Use 4000 Surveyed (Mail List) 9 Responded 5 Used None GUI Builders Netbeans Borland IDEA Win Builder None 2008 JavaOne SM Conference java.sun.com/javaone 36
Resources Folks & Sites I stole reused code from Swing Hacks (9780596009076) Joshua Marinacci, Chris Adamson Alexander Potochkin's Blog http://weblogs.java.net/blog/alexfromsun/ Filthy Rich Clients Chet Haase and Romain Guy http://filthyrichclients.org/ http://community.java.net/javadesktop/ 2008 JavaOne SM Conference java.sun.com/javaone 37
Summary If you walk out with one message today: Swing is a great tool, there are lots of resources to help you You now have: A guideline for evaluating Frameworks and a example of a reasonable one A feel for some common issues in Swing Development A list of good resources to go and learn/try more 2008 JavaOne SM Conference java.sun.com/javaone 38
Brian Mason, Dir Software Engineering. S295599 2008 JavaOne SM Conference java.sun.com/javaone 39