SWT Snippets
Snippets are minimal stand-alone programs that demonstrate specific techniques or functionality. Often a small example is the easiest way to understand how to use a particular feature. (If you are looking for large examples, like ControlExample, see the SWT Examples, and if you are programming with JFace, you may find these JFace Snippets useful).
Snippets also help isolate problems. The best way to report an SWT bug is to write your own snippet showing the problem and paste it into the bug report. For a snippet template, see the "Hello World" example.
Note that the examples here are often edited for brevity rather than completeness. They are intended to guide the reader towards the correct solution, rather than be finished products. These snippets are tested against the HEAD stream and may sometimes reference new API or require bug fixes from there.
To run a snippet, simply import SWT into your Eclipse workspace, create a new Java project that depends on SWT, copy the desired snippet to the clipboard, and paste it into a new snippet class. (If you are using eclipse 3.2 M1 or earlier, you need to create the class using the New Class wizard before pasting; but since 3.2 M1 you can simply select your project and paste, and the class is created for you). Run by selecting the class and then selecting Run > Run As > Java Application.
To contribute a new snippet, create a snippet contribution report in Bugzilla. Thanks in advance for your contribution!
- "Hello World"
- Accessibility
- using an accessible listener to provide state information – (preview)
- provide text that will be spoken for an image button – (preview)
- give accessible names to a tree and its tree items – (preview)
- respond to text-based questions from an AT – (preview)
- tell a screen reader about updates to a non-focused descriptive area – (preview)
- use accessible relations to provide additional information to an AT – (preview)
- provide a way for an AT to set text attributes in a StyledText – (preview)
- declare a message area to be a "live region" – (preview)
- Browser
- check if the browser is available or not – (preview)
- bring up a browser (single window) – (preview)
- bring up a browser with pop-up blocker – (preview)
- bring up a browser with pop-up window support – (preview)
- render HTML from memory – (preview)
- render HTML that includes relative links from memory – (preview)
- render HTML from memory in response to a link click – (preview)
- listen for DOM mousedown events with JavaScript (SWT 3.4 and earlier) – (preview)
- listen for DOM mousedown events with JavaScript (SWT 3.5 and newer) – (preview)
- modify DOM (execute JavaScript) – (preview)
- query DOM node value (SWT 3.4 and earlier) – (preview)
- query DOM node value (SWT 3.5 and newer) – (preview)
- modify HTML title tag – (preview)
- provide credentials for a basic authentication challenge – (preview)
- call Java from JavaScript – (preview)
- close a Browser such that it can be cancelled by an onbeforeunload handler – (preview)
- send custom headers and post data with HTTP requests – (preview)
- WebKit in a browser – (preview)
- Resize the shell to the fit the html content after it's loaded – (preview)
- Mozilla in a browser
- toggle a Mozilla Browser between Design mode and View mode
- implement a custom download handler for a Mozilla Browser
- show an HTML element-specific context menu in a Mozilla Browser
- examine request and response headers in a Mozilla Browser
- view DOM tree and edit node values in a Mozilla Browser
- zoom HTML page content in a Mozilla Browser
- BusyIndicator
- Canvas
- Caret
- CCombo
- Clipboard
- Color and RGB
- Combo
- Composite
- scroll a child control automatically – (preview)
- set the tab traversal order of children – (preview)
- create and dispose children of a composite – (preview)
- force radio behavior on two different composites – (preview)
- intercept mouse events (drag a button with the mouse) – (preview)
- inherit a background color or image – (preview)
- Control
- detect mouse enter, exit and hover events
- prevent Tab from traversing out of a control – (preview)
- allow a multi-line text to process the default button – (preview)
- perform custom traversals – (preview)
- print key state, code and character – (preview)
- print mouse state and button (down, move, up)
- print mouse events (all) – (preview)
- set a background image (a dynamic gradient) – (preview)
- set transparent background – (preview)
- CoolBar
- CTabFolder, CTabItem
- Cursor
- DateTime
- DirectoryDialog
- Display
- create two one shot timers (5000 ms, 2000 ms)
- create one repeating timer (every 500 ms)
- stop a repeating timer when a button is pressed – (preview)
- get the bounds and client area of a display
- fill a table from a background thread (sync exec) – (preview)
- post mouse events (UI testing tools only) – (preview)
- post key events (UI testing tools only) – (preview)
- post key events to simulate CTRL+END (UI testing tools only) – (preview)
- post mouse wheel events (UI testing tools only) – (preview)
- detect a system settings change – (preview)
- map from control-relative to display-relative coordinates – (preview)
- use application menu bar, if platform supports it – (preview)
- more complex example using application menu bar to provide better platform behavior – (preview)
- Drag and Drop
- drag text between two labels – (preview)
- drag leaf items in a tree – (preview)
- define my own data transfer type – (preview)
- define a second data transfer type that subclasses the first – (preview)
- define a default operation (in this example, Copy) – (preview)
- determine data types available (win32 only) – (preview)
- make a dropped data type depend on a target item in table – (preview)
- drag text between two StyledText widgets – (preview)
- drag text within a StyledText widget (with drag image) – (preview)
- detect drag in a custom control – (preview)
- drag and drop a URL – (preview)
- drag and drop text between SWT and Swing – (preview)
- drag and drop an object with a custom data transfer type from SWT to AWT/Swing – (preview)
- ExpandBar
- FileDialog
- Font
- FormLayout
- create a simple dialog using form layout – (preview)
- center a label and single line text using a form layout – (preview)
- create a simple OK/Cancel dialog using form layout – (preview)
- create a simple text prompt dialog using form layout – (preview)
- use a form layout to hide/show a composite – (preview)
- GC
- create an icon (in memory) – (preview)
- capture a widget image with a GC – (preview)
- draw a multi-gradient (without advanced graphics) – (preview)
- draw a multi-gradient – (preview)
- draw a thick line – (preview)
- draw lines with different cap and join styles – (preview)
- draw using transformations, paths and alpha blending – (preview)
- implement a simple scribble program – (preview)
- measure a string – (preview)
- reflect, shear and rotate images using matrix transformations – (preview)
- take a screen shot with a GC – (preview)
- draw 2 polylines with different line attributes – (preview)
- Gesture, Touch support
- GridLayout
- align widgets in rows and columns – (preview)
- insert widgets into a grid layout – (preview)
- span columns and center within columns – (preview)
- exclude an invisible widget from a grid layout – (preview)
- show a wrappable Label that fills available width without expanding it – (preview)
- grow/shrink a wrappable Text's height to show its content as it changes – (preview)
- Image
- ImageData
- Label
- Link
- List
- Monitor
- OLE and ActiveX
- get events from IE control – (preview)
- browse the typelibinfo for a program id
- embed Word in an applet
- reading and writing to a SAFEARRAY
- execute a script function that exists on a web page – (preview)
- listen for Excel AppEvents
- open an Excel Sheet
- open a Word document
- open a PowerPoint slide
- open a Media Player – (preview)
- open a Windows Media Player – (preview)
- show selected cell position in Excel
- OpenGL
- Path, Pattern
- Printing
- Program
- ProgressBar
- RowLayout
- Sash
- SashForm
- Scale
- ScrolledComposite
- scroll a control in a scrolled composite – (preview)
- create a ScrolledComposite with wrapping content – (preview)
- create two ScrolledComposites that scroll in tandem – (preview)
- scroll a widget into view on focus in – (preview)
- use a ScrolledComposite to scroll a Tree vertically – (preview)
- use a ScrolledComposite to scroll a disabled control – (preview)
- Shell
- create a dialog shell
- create a dialog shell (prompt for a value) – (preview)
- create a dialog shell (prompt for a string) – (preview)
- create a dialog shell and position it – (preview)
- create a splash screen – (preview)
- prevent a shell from closing (prompt the user) – (preview)
- allow escape to close a shell – (preview)
- prevent escape from closing a dialog – (preview)
- open a shell minimized (iconified)
- open a shell maximized
- create a non-rectangular window – (preview)
- create a circular shell from a path – (preview)
- set icons with different resolutions – (preview)
- emulate transparent shell – (preview)
- create a non-rectangular shell from a transparent image – (preview)
- use shell modified state to prompt for save – (preview)
- Slider
- Spinner
- StackLayout
- StyledText
- change font style, foreground and background colors of StyledText – (preview)
- draw a box around text – (preview)
- underline and strike through text – (preview)
- use rise and font with StyleRange – (preview)
- embed images in StyledText – (preview)
- use indent, alignment and justify – (preview)
- embed controls in StyledText – (preview)
- use bulleted lists in StyledText – (preview)
- use gradient background in StyledText – (preview)
- variable tab stops in StyledText – (preview)
- per line variable tab stops in StyledText – (preview)
- different types of indent and combining wrap indent and bulleted lists – (preview)
- using BidiSegmentEvent#segmentsChars to implement custom bidi segments – (preview)
- embed an inline link in StyledText – (preview)
- line spacing provider in StyledText – (preview)
- dynamically customize line spacing provider in StyledText – (preview)
- ligatures support added in StyledText – (preview)
- Swing/AWT
- embed Swing/AWT in SWT – (preview)
- embed a JTable in SWT (no flickering) – (preview)
- draw an X using AWT Graphics – (preview)
- convert between SWT Image and AWT BufferedImage – (preview)
- launch SWT from AWT and keep both active – (preview)
- drag and drop an object with a custom data transfer type from SWT to AWT/Swing – (preview)
- use Java2D to modify an image being displayed in an SWT GUI – (preview)
- TabFolder, TabItem
- Table, TableItem, TableColumn
- add 1000 sorted entries to a virtual table every 500 ms – (preview)
- color cells and rows in table – (preview)
- create a table (no columns, no headers) – (preview)
- create a table (columns, headers, lines) – (preview)
- create a table with column header images – (preview)
- create a table with 1,000,000 items (lazy) – (preview)
- create a table with 1,000,000 items (lazy, page size 64) – (preview)
- create a table (lazy with a thread) – (preview)
- create a table with no scroll bars – (preview)
- detect a selection or check event in a table (SWT.CHECK) – (preview)
- find a table cell from mouse down (SWT.FULL_SELECTION) – (preview)
- find a table cell from mouse down (works for any table style) – (preview)
- insert a table item (at an index) – (preview)
- insert a table column (at an index) – (preview)
- allow user to reorder columns by dragging or programmatically – (preview)
- place arbitrary controls in a table – (preview)
- print selected items in a table – (preview)
- remove selected items – (preview)
- resize columns as table resizes – (preview)
- scroll a table one "page" at a time (set the top index) – (preview)
- select an index (select and scroll) – (preview)
- sort a table by column – (preview)
- sort a table by column (virtual table, sort indicator) – (preview)
- update table item text – (preview)
- show a table with images of various sizes – (preview)
- draw images on right side of table item – (preview)
- show results as a bar chart in table – (preview)
- draw a custom gradient selection for table – (preview)
- draw multiple lines in a table item – (preview)
- fixed first column horizontal scroll remaining columns – (preview)
- draw different foreground colors for text in a TableItem – (preview)
- make text span multiple columns – (preview)
- compute the number of visible rows in a table – (preview)
- specify custom content dimensions in a table with no columns – (preview)
- specify custom content widths when a column is packed – (preview)
- modify the clipping of custom background paints – (preview)
- show a menu in a table header – (preview)
- TableCursor
- TableEditor
- TaskBar, TaskBarItem
- Text
- add a select all menu item to the control – (preview)
- detect CR in a text control (default selection) – (preview)
- override Tab behavior to traverse out of a text control – (preview)
- prevent CR from going to the default button – (preview)
- prompt for a password (set the echo character) – (preview)
- resize a text control (show about 10 characters) – (preview)
- select all the text in the control – (preview)
- set the selection (i-beam) – (preview)
- set the selection (start, end) – (preview)
- verify input (only allow digits) – (preview)
- verify input in a template (YYYY/MM/DD) – (preview)
- detect when the user scrolls a text control – (preview)
- verify input using a regular expression – (preview)
- type in one text control, output to another – (preview)
- create a search text control – (preview)
- display different styles of the search Text control – (preview)
- implement content assist – (preview)
- set contents of a password field using a char array instead of a String – (preview)
- TextLayout, TextStyle
- draw internationalized styled text on a shell – (preview)
- draw dynamically wrapped text on a shell – (preview)
- justify, align and indent text – (preview)
- change the rise of text relative to the baseline – (preview)
- embed images and widgets in text – (preview)
- underline and strike through text – (preview)
- ToolBar, ToolItem
- create a tool bar (text) – (preview)
- create a flat tool bar (images) – (preview)
- create tool bar (normal, hot and disabled images) – (preview)
- create tool bar (wrap on resize) – (preview)
- place a combo box in a tool bar – (preview)
- place a drop down menu in a tool bar – (preview)
- update a status line when the mouse enters a tool item – (preview)
- create a tool bar containing animated gifs – (preview)
- Tool Tips
- Tracker
- Tray, TrayItem
- Tree, TreeItem, TreeColumn
- create a tree – (preview)
- create a tree (lazy) – (preview)
- create a virtual tree (lazy) – (preview)
- create a tree with no scroll bars – (preview)
- detect a selection or check event in a tree (SWT.CHECK) – (preview)
- implement standard tree check box behavior (SWT.CHECK) – (preview)
- insert a tree item (at an index) – (preview)
- print selected items in a tree – (preview)
- limit selection to items that match a pattern – (preview)
- detect mouse down in a tree item – (preview)
- create a tree with columns – (preview)
- make columns reorderable by dragging – (preview)
- images on right side of TreeItem – (preview)
- show results as a bar chart in tree – (preview)
- draw a custom gradient selection for tree – (preview)
- draw multiple lines in a tree item – (preview)
- scroll tree when mouse at top or bottom – (preview)
- search for a string in a tree (recursively) – (preview)
- compute the number of visible rows in a tree – (preview)
- show a menu in a tree header – (preview)
- determine which TreeItems are visible in a Tree's viewport – (preview)
- TreeCursor
- TreeEditor
- ... and lots more still to come. Keep checking back.