110 likes | 250 Views
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
E N D
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.html
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)