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 as a pull request. Thanks in advance for your contribution!
- 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)
- 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
- set the default button – (preview)
- make a toggle button have radio behavior – (preview)
- implement radio behavior for setSelection() – (preview)
- a button with text and image – (preview)
- create a tri-state button – (preview)
- create a tri-state button (toggle three states) – (preview)
- create a non-rectangular button – (preview)
- create buttons with wrapped titles – (preview)
- paint a circle in a canvas – (preview)
- scroll an image (flicker free, no double buffering) – (preview)
- implement tab traversal (behave like a tab group) – (preview)
- update a portion of a Canvas frequently – (preview)
- ignore 2nd mouse up event after double-click
- copy and paste data with the clipboard – (preview)
- copy and paste image with the clipboard using ImageTransfer – (preview)
- enable/disable menu depending on clipboard content availability – (preview)
- create a combo box (non-editable) – (preview)
- detect return in a combo box (default selection) – (preview)
- prevent CR from going to the default button – (preview)
- set the caret position within a combo's text – (preview)
- add a new number item to a combo box – (preview)
- 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)
- 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)
- create a cool bar – (preview)
- create a cool bar (relayout when resized) – (preview)
- drop-down a chevron menu containing hidden tool items – (preview)
- prevent an item from closing – (preview)
- min and max buttons, close button and image only on selected tab – (preview)
- demonstration of a multi line CTabFolder – (preview)
- set the hand cursor into a control – (preview)
- create a cursor from a source and a mask – (preview)
- create a color cursor from a source and a mask – (preview)
- create a color cursor from an image file – (preview)
- hide the Cursor over a control – (preview)
- create a DateTime calendar and a DateTime time – (preview)
- create a DateTime calendar and a DateTime time in a dialog – (preview)
- DateTime with localization support – (preview)
- 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 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)
- create an expand bar – (preview)
- change an ExpandItem's height in response to a change in a contained Control – (preview)
- 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)
- 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)
- draw lines with configurable line width, scaling and rotation – (preview)
- 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)
- display an image in a group – (preview)
- draw a reflection of an image – (preview)
- draw an image scaled to half size and double size – (preview)
- draw an image at various zoom/dpi levels – (preview)
- draw a disabled/grayed image at various zoom levels – (preview)
- create a bar and pull down menu (accelerators, mnemonics) – (preview)
- create a popup menu (set in multiple controls) – (preview)
- create a popup menu with a submenu – (preview)
- create a menu with radio items – (preview)
- enable menu items dynamically (when menu shown) – (preview)
- fill a menu dynamically (when menu shown) – (preview)
- show a popup menu (wait for it to close) – (preview)
- update a status line when an item is armed – (preview)
- use a menu item's armListener to update a status line – (preview)
- get events from IE control – (preview)
- browse the typelibinfo for a program id
- 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
- draw a rotating torus using the LWJGL OpenGL binding – (preview)
- draw a rotating torus using the JOGL OpenGL binding – (preview)
- draw a rectangle using the org.eclipse.opengl OpenGL binding
- capture a LWJGL drawing to an SWT Image – (preview)
- create a path from some text – (preview)
- create a circular shell from a path – (preview)
- fill a shape with a predefined pattern – (preview)
- print "Hello World!" in black, outlined in red, to default printer
- print text to printer, with word wrap and pagination – (preview)
- print a control to the printer – (preview)
- print in landscape mode
- find the icon of the program that edits .bmp files – (preview)
- invoke an external batch file
- invoke the system text editor on a new file
- update a progress bar (from the UI thread) – (preview)
- update a progress bar (from another thread) – (preview)
- create a sash (allow it to be moved) – (preview)
- implement a simple splitter (with a 20 pixel limit) – (preview)
- 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)
- 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)
- create and initialize a spinner widget – (preview)
- use floats in a spinner widget – (preview)
- validate input in a spinner widget – (preview)
- 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)
- 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)
- 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)
- edit the text of a table item (in place) – (preview)
- edit a cell in a table (in place, fancy) – (preview)
- place a progress bar in a table – (preview)
- 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)
- 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)
- 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)
- create tool tips for a tab folder, tool bar and control – (preview)
- create emulated tool tips for items in a table – (preview)
- create a balloon tooltip for a tray item – (preview)
- show a tool tip iff it's not fully visible – (preview)
- show a tool tip inside a rectangle – (preview)
- 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)