Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Standard Widget Toolkit
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Design == [[File:Eclipse SWT Linux example.png|thumb|212px|A demo application on Linux]] SWT is a wrapper around native code objects, such as [[GTK]] objects, [[Motif (software)|Motif]] objects etc. Because of this, SWT widgets are often referred to{{By whom|date=May 2010}} as "heavyweight", evoking images of a light Java wrapper around a "heavy" native object. In cases where native platform GUI libraries do not support the functionality required for SWT, SWT implements its own GUI code in Java, similar to Swing. In essence, SWT is a compromise between the low-level performance and look and feel of AWT and the high-level ease of use of Swing.<ref>{{cite web |author= Steve Northover |title= SWT: Implementation Strategy for Java Natives |url= http://www.eclipse.org/articles/Article-SWT-Design-1/SWT-Design-1.html |access-date= 2001-03-22}}</ref><ref>{{cite web |author1=Carolyn MacLeod |author2=Steve Northover |name-list-style=amp |title= SWT: Managing Operating System Resources |url= http://www.eclipse.org/articles/swt-design-2/swt-design-2.html |access-date= 2001-11-27}}</ref> According to the Eclipse Foundation, "SWT and Swing are different tools that were built with different goals in mind. The purpose of SWT is to provide a common API for accessing native widgets across a spectrum of platforms. The primary design goals are high performance, native look and feel, and deep platform integration. Swing, on the other hand, is designed to allow for a highly customizable look and feel that is common across all platforms."<ref name="swtbetterthanswing">{{cite web |title= FAQ: Is SWT better than Swing? |url= http://wiki.eclipse.org/FAQ_Is_SWT_better_than_Swing%3F |access-date= 2008-02-16}}</ref> It has been argued{{By whom|date=May 2010}} that SWT features a clean design, in part inspired by [[Erich Gamma]] of [[Design Patterns]] fame.<ref>{{cite web |author= Ben Galbraith |title= An Introduction to SWT |url= http://www.javalobby.org/eps/swt_intro/ |access-date= 2007-03-24}}{{dead link|date=December 2024|bot=medic}}{{cbignore|bot=medic}}</ref> SWT is a simpler toolkit than Swing, with less (possibly) extraneous functionality for the average developer.<ref name="goslingqa">{{cite web |author= Ella Morton |title= James Gosling Q & A |url= http://www.builderau.com.au/program/work/0,39024650,39176462,00.htm |access-date= 2007-03-24 |url-status= dead |archive-url= https://web.archive.org/web/20060830151351/http://www.builderau.com.au/program/work/0%2C39024650%2C39176462%2C00.htm |archive-date= 2006-08-30 }}</ref> This has led some people{{Who|date=May 2010}} to argue that SWT lacks functionality when compared to Swing.<ref name="slashdotperformancebenchmarks">{{cite web |title= Performance Benchmarks of Nine Languages |url= http://developers.slashdot.org/article.pl?sid=04/01/09/1340223 |access-date= 2007-03-24}}</ref> [[James Gosling]], the creator of the Java language, has argued that SWT is too simple, and is a difficult toolkit to port to new platforms for the same reason that AWT once had porting problems: that it is too simple, too low level, and too tied to the Win32 GUI API, leading to problems adapting the SWT API to other GUI toolkits, such as Motif and OS X Carbon.<ref name="goslingqa"/> Although SWT does not implement the popular [[model–view–controller]] (MVC) architecture used in Swing and multiple other high-level GUI toolkits, the [[JFace]] library, which is developed as part of the same Eclipse project, does provide a [[cross-platform]], higher-level MVC abstraction atop SWT. Developers may choose to use JFace to provide more flexible and abstract data models for complex SWT controls such as trees, tables, and lists, or access those controls directly as needed. === Look and feel === [[File:Eclipse SWT Mac example.png|thumb|The same demo application on macOS]] SWT widgets have the same ''[[look and feel]]'' as native widgets because they often are the same native widgets. This is in contrast to the Swing toolkit where all widgets are emulations of native widgets. In some cases the difference is distinguishable. For example, the [[macOS]] tree widget features a subtle animation when a tree is expanded and default buttons have an animated pulsing glow to focus the user's attention on them. The default Swing version of these widgets does not animate. Since SWT is simply a wrapper around native GUI code, it does not require large numbers of updates when that native code is changed, providing that operating system vendors are careful not to break clients of their API when the operating systems are updated. The same cannot be said of Swing, which supports the ability to change the look and feel of the running application with "pluggable looks and feels". These enable emulating the native platform user interface using [[theme (computing)|theme]]s, which must be updated to mirror operating system GUI changes, such as theme or other look and feel updates. SWT aims for "deep platform integration", the Eclipse reference to SWT's use of native widgets. According to Mauro Marinillia of developer.com, "whenever one needs a tight integration with the native platform, SWT can be a plus".<ref name="taleof2guis">{{cite web |title= Swing and SWT: A Tale of Two Java GUI Libraries |url= http://www.developer.com/java/other/article.php/2179061 |first= Mauro |last= Marinilli |access-date= 2006-11-07}}</ref> This deep integration can be useful in a number of ways, for example enabling SWT to wrap [[ActiveX]] objects on [[Microsoft Windows]]. === Programming === [[File:Screenshot-swt-helloworld.png|thumb|A simple GUI application using SWT running in a [[GTK]] environment]] The following is a basic [["Hello, World!" program]] using SWT. It shows a window (''Shell'') and a label. <syntaxhighlight lang="java"> import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class HelloWorld { public static void main (String[] args) { Display display = new Display(); Shell shell = new Shell(display); Label label = new Label(shell, SWT.NONE); label.setText("Hello, World!"); label.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } </syntaxhighlight> Contrary to [[Swing (Java)|Swing]], a ''Display'' class is necessary to access the underlying [[operating system]], and its resources must be explicitly disposed of when they are no longer used. === Platform support === [[File:Azureus screenshot.png|thumb|[[Vuze]], a [[BitTorrent client]] which uses SWT]] SWT must be ported to every new GUI library that needs supporting. Unlike Swing and AWT, SWT is not available on every Java-supported platform since SWT is not part of the Java release. There is also some evidence that the performance of SWT on platforms other than Windows is noticeably less efficient.<ref name="slashdotperformancebenchmarks" /> Since SWT uses a different native library for each platform, SWT programs may be exposed to platform-specific bugs. SWT exposes programs to more low-level details than Swing. This is because SWT is technically just a layer over native library provided GUI functionality, exposing the programmer to native GUI code is part of the design intent of SWT: "Its goal is not to provide a rich user-interface design framework but rather the thinnest possible user-interface API that can be implemented uniformly on the largest possible set of platforms while still providing sufficient functionality to build rich graphical user interface (GUI) applications."<ref>{{cite web |url=http://wiki.eclipse.org/index.php/FAQ_What_is_SWT%3F |title=FAQ What is SWT |work= Eclipsepedia |publisher=eclipse.org |access-date=2009-10-16}}</ref> Since the SWT implementation is different for each platform, a platform-specific SWT library (JAR file) must be distributed with each application. {{As of|2018}}, SWT supports these platforms and/or GUI libraries:<ref>{{Cite web|url=http://download.eclipse.org/eclipse/downloads/drops4/S-4.8M6-201803080630/|title=4.8M6 - Eclipse Project Downloads|website=download.eclipse.org|language=en|access-date=2018-05-01}}</ref> * <!--Not just; down to WinXP?:-->[[Windows 10|Windows]]:<ref>{{Cite web|url=https://wiki.eclipse.org/Platform_UI/Testing|title=Platform UI/Testing - Eclipsepedia|website=wiki.eclipse.org|language=en|access-date=2018-05-01}}</ref> ** [[Win32]] ** [[Windows Presentation Foundation]] (WPF), under development * [[Unix-like]]: [[Linux]], <!--not officially listed, I believe would work:-->[[FreeBSD]]: ** [[GTK]] <!--Too dubious, need a current source; was supported: ** [[Motif (software)|Motif]]--> * [[macOS]]: ** [[Cocoa (API)|Cocoa]] <!--dubious? , was supported previously only? * [[Pocket PC]]--> {{As of|2018|03}}, SWT 4.7.3a (and 4.8M6) is officially compatible with the following operating systems (graphic library or similar if explicitly required / processors):<ref>{{cite web |url=http://download.eclipse.org/eclipse/downloads/drops4/R-4.7.3a-201803300640/ |title=4.7.3a - Eclipse Project Downloads |website=download.eclipse.org |url-status=dead |archive-url=https://web.archive.org/web/20180416130409/http://download.eclipse.org/eclipse/downloads/drops4/R-4.7.3a-201803300640 |archive-date=2018-04-16}} </ref> * Microsoft Windows (x86 and x86_64) * Linux (GTK / PPC64 and PPC64LE) * macOS (Cocoa / x86_64) [[File:Eclipse SWT Windows XP example.png|thumb|Example application on Windows XP]] [[Windows XP]] has historically been supported<!--unclear if the current version does 4.7.3a docs don't state and but this Wikipedia page did list it:--> as have Linux on [[ESA/390|s390]], [[Solaris 11]] (SPARCv9), [[Solaris 10]] (x86_64), [[HP-UX]] (ia64), [[IBM AIX|AIX]] (PPC and PPC64).<ref>{{Cite web|url=http://archive.eclipse.org/eclipse/downloads/drops4/R-4.6.3-201703010400/|title=4.6.3 - Eclipse Project Downloads|website=archive.eclipse.org|language=en|access-date=2018-05-01}}</ref> === Performance === SWT was designed to be a ''high performance'' GUI toolkit; faster, more responsive and lighter on system resource usage than Swing.<ref>{{cite web |url= http://weblogs.java.net/blog/aiqa/archive/2004/11/why_i_choose_sw.html |title= Why I choose SWT against Swing |first= Ozgur |last= Akan |date= November 19, 2004 |access-date= 2006-11-07 |url-status= dead |archive-url= https://web.archive.org/web/20061231140431/http://weblogs.java.net/blog/aiqa/archive/2004/11/why_i_choose_sw.html |archive-date= December 31, 2006 }}</ref> There has been some attempted [[benchmark (computing)|benchmarking]] of SWT and Swing, which concluded that SWT should be more efficient than Swing, although the applications benchmarked in this case were not complex enough to draw solid conclusions for all possible SWT or Swing uses.<ref>{{cite web |url=http://www.javalobby.org/java/forums/t65168.html |title=Swing vs. SWT Performance – Have a Look at the Call Stacks |publisher=Javalobby.org |date=2006-03-03 |access-date=2009-10-16 |archive-date=2017-09-17 |archive-url=https://web.archive.org/web/20170917172003/http://www.javalobby.org/java/forums/t65168.html |url-status=usurped }}.</ref> A fairly thorough set of benchmarks concluded that neither Swing nor SWT outperformed the other in the general case.<ref>{{Cite web |url=http://cosylib.cosylab.com/pub/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf |title=SWT Vs. Swing Performance Comparison |first=Križnar |last=Igor |publisher=cosylab.com |date=2005-05-10 |quote=''It is hard to give a rule-of-thumb where SWT would outperform Swing, or vice versa. In some environments (e.g., Windows), SWT is a winner. In others (Linux, [[VMware]] hosting Windows), Swing and its redraw optimization outperform SWT significantly. Differences in performance are significant: factors of 2 and more are common, in either direction.'' |access-date=2008-05-24 |url-status=dead |archive-url=https://web.archive.org/web/20080704103309/http://cosylib.cosylab.com/pub/CSS/DOC-SWT_Vs._Swing_Performance_Comparison.pdf |archive-date=2008-07-04 }}.</ref> === Extensibility and comparison to other Java code === Due to the use of native code, SWT classes do not allow for easy inheritance for all widget classes, which some users consider can hurt extensibility.<ref name= "taleof2guis" /> This can make customizing existing widgets more difficult to achieve with SWT than if one were using Swing.<ref name="creatingSWTWidgets" >{{cite web |url= http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm |title= Creating Your Own Widgets using SWT |publisher=eclipse.org |date= 2007-03-22 |quote=''Subclassing may cause bad system-level bugs, and runs the risk of leaking resources(...)Subclassing Canvas or Composite is the best way to ensure that your widget works on all SWT platforms(...)When subclassing anything other than Composite or Canvas you must override the method protected void checkSubclass() to do nothing'' |access-date= 2008-12-13}}</ref> Both toolkits support writing new widgets using only Java code, however in SWT extra work is needed to make the new widget work on every platform.<ref name="creatingSWTWidgets" /> SWT widgets, unlike almost any other Java toolkit, requires manual object deallocation, in contrast to the standard Java practice of [[Garbage collection (computer science)|automatic garbage collection]]. SWT objects must be explicitly deallocated using the <code>dispose</code> method, which is analogous to the [[C (programming language)|C language]]'s <code>free</code>.<ref>The Java developers guide to Eclipse, 2nd ed., p359</ref> If this is not done, [[memory leak]]s or other unintended behavior may result. On this matter, some have commented that "explicitly de-allocating the resources could be a step back in development time (and costs) at least for the average Java developer" and that "this is a mixed blessing. It means more control (and more complexity) for the SWT developer instead of more automation (and slowness) when using Swing."<ref name="taleof2guis" /> The need for manual object deallocation when using SWT is largely due to SWT's use of native objects. These objects are not tracked by the Java JVM, so it cannot track whether or not such objects are in use, and thus cannot garbage collect them at a suitable time.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)