SWING 101.0 IF YOU GET LOST - IMPORTANT LINKS Swing articles: http://java.sun.com/javase/technologies/desktop/articles.jsp Swing Architecture: http://java.sun.com/products/jfc/tsc/articles/architecture/ Visual Editor for Eclipse: http://wiki.eclipse.org/VE/Update#Online_Install Oracle Swing tutorial: http://download.oracle.com/javase/tutorial/uiswing/ Stack Overflow: http://stackoverflow.com/ Basic Swing tutorial: http://zetcode.com/tutorials/javaswingtutorial/ SWING COMPONENTS HIERARCHY Tutorial http://www.docstoc.com/docs/33446046/Java-Swing# Components http://download.oracle.com/javase/tutorial/ui/features/compWin.html Layout http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html PAINT AND LAYOUT Swing components painting and layout order First you set properties on components Then swings lays outs components Then paints them Note: until the top level container has been set to visible and laid out you can not know the size and location of your component paint, repaint, invalidate, validate paint() – paints the component. Invoked internally by Swing – DO NOT USE repaint() – instructs Swing to paint the given region when the “time is right” – Safe to use validate() –checks if the container (and its children) have been changed and if so instructs Swing to perform a re-layout invalidate() –marks a container as invalid but does not perform a re-layout CONTAINERS The main containers are: JPanel, JSplitPane, JScrollPane and JTabbedPane Have properties that defines their looks and behavior (just like components) Don’t have data models Have events (mouse events, size changes events, etc.) Adding components– add the component to the children list of the container and set the component parent to be the container When you add a component you link it to the container – not create a copy of it in the container getParent() – for containers and components getChildren() – for containers only Each component or container has a single parent LAYOUT MANAGER Only valid for containers Automatically set the size and location of all the child components in the container Different layout algorithms – some have properties (for example: gap between components) Some require additional data for each child component – that data should be added in advanced when adding a component to a container Settings size constrains (size, preferred, minimum, maximum) http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html COMPLEX LAYOUT A container an contains child containers (same as components) When it resizes – all of its child containers will be resized as well and then re-layout their child component recursively You can mix different layout algorithms (for example: a JPanel with Border Layout that contains 3 JPanels with Flow Layout each) ACTIONS Just like the interface Runnable – this is Swing’s way to pass “a parameter of a method to a component” – meaning, pass some code to execute when a common event occurs Actions are not JComponents (not UI objects) The same action instance can be passed to several UI components (menu item, JButton, etc.) Actions have properties for the component that will use them: Label Icon Tooltip DIALOGS JDialogs are Top-Level containers Open a dialog that displayed some content (like JFrame) To set custom content – add it to the dialog’s content pane After calling setVisible(true) on a dialog – no events are dispatched to the rest of the UI components (except in the dialog itself) You can customize the dialog’s buttons (change the number of buttons, the text on each button, etc.) http://download.oracle.com/javase/tutorial/uiswing/components/dialog.ht ml GUI EDITORS Great for all the things in the world… except when you need to change a component programmatically Here are some recommendations: NetBeans JFormDesigner Eclipse UI editor Another issue – some editors might add dependencies on external Jars (which you’ll need to bundle with your complied code in order for it to execute) Adding external jars to project OTHER SWING STUFF Menus Renderers (and Editors) How to set native look and feel Selection events, change events(Optional)