Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
开发者_开发知识库Closed 9 years ago.
Improve this questionI'm a web developer at day and thinking about building my first real desktop application. The idea is to build a tool that automates a very repetitive task in a web application where no API is available.
I know I want to use Java. I used it before for web stuff, know the syntax pretty well and want the application to be cross plattform as easy as possible.
Where I'm not so sure is if I should use SWT or Swing. As my main audience uses Windows, I want to look it as native as possible there. Linux and Mac should work, but the looks are not so important here.
So what are the arguments for and against each UI Framework, Swing or SWT?
Thanks.
PS: I develop on Windows using Eclipse. But was thinking about playing with Netbeans.
Pros Swing:
- part of java library, no need for additional native libraries
- works the same way on all platforms
- Integrated GUI Editor in Netbeans and Eclipse
- good online tutorials by Sun/Oracle
- Supported by official java extensions (like java OpenGL)
Cons Swing:
- Native look and feel may behave different from the real native system.
- heavy components (native/awt) hide swing components, not a problem most of the time as as use of heavy components is rather rare
Pros SWT:
- uses native elements when possible, so always native behavior
- supported by eclipse, gui editor VEP (VEP also supports Swing and AWT)
- large number of examples online
- has an integrated awt/swt bridge to allow use of awt and swing components
Cons SWT:
- requires native libraries for each supported system
- may not support every behavior on all systems because of native resources used (hint options)
- managing native resources, while native components will often be disposed with their parent other resources such as Fonts have to be manually released or registered as dispose listener to a component for automatic release.
An important thing to consider is that some users and some resellers (Dell) install a 64 bit VM on their 64 bit Windows, and you can't use the same SWT library on 32 bit and 64 bit VMs.
This means you will need to distribute and test different packages depending on whether users have 32-bit or a 64-bit Java VM. See this problem with Azureus, for instance, but you also have it with Eclipse, where as of today the builds on the front download page do not run on a 64 bit VM.
pro swing:
- The biggest advantage of swing IMHO is that you do not need to ship the libraries with you application (which avoids dozen of MB(!)).
- Native look and feel is much better for swing than in the early years
- performance is comparable to swt (swing is not slow!)
- NetBeans offers Matisse as a comfortable component builder.
- The integration of Swing components within JavaFX is easier.
But at the bottom line I wouldn't suggest to use 'pure' swing or swt ;-) There are several application frameworks for swing/swt out. Look here. The biggest players are netbeans (swing) and eclipse (swt). Another nice framework could be griffon and a nice 'set of components' is pivot (swing). Griffon is very interesting because it integrates a lot of libraries and not only swing; also pivot, swt, etc
I would use Swing for a couple of reasons.
It has been around longer and has had more development effort applied to it. Hence it is likely more feature complete and (maybe) has fewer bugs.
There is lots of documentation and other guidance on producing performant applications.
- It seems like changes to Swing propagate to all platforms simultaneously while changes to SWT seem to appear on Windows first, then Linux.
If you want to build a very feature-rich application, you might want to check out the NetBeans RCP (Rich Client Platform). There's a learning curve, but you can put together nice applications quickly with a little practice. I don't have enough experience with the Eclipse platform to make a valid judgment.
If you don't want to use the entire RCP, NetBeans also has many useful components that can be pulled out and used independently.
One other word of advice, look into different layout managers. They tripped me up for a long time when I was learning. Some of the best aren't even in the standard library. The MigLayout (for both Swing and SWT) and JGoodies Forms tools are two of the best in my opinion.
I whould choose swing just because it's "native" for java.
Plus, have a look at http://swingx.java.net/.
For your requirements it sounds like the bottom line will be to use Swing since it is slightly easier to get started with and not as tightly integrated to the native platform as SWT.
Swing usually is a safe bet.
Interesting question. I don't know SWT too well to brag about it (unlike Swing and AWT) but here's the comparison done on SWT/Swing/AWT.
http://www.developer.com/java/other/article.php/10936_2179061_2/Swing-and-SWT-A-Tale-of-Two-Java-GUI-Libraries.htm
And here's the site where you can get tutorial on basically anything on SWT (http://www.java2s.com/Tutorial/Java/0280__SWT/Catalog0280__SWT.htm)
Hope you make a right decision (if there are right decisions in coding)... :-)
One thing to consider: Screenreaders
For some reasons, some Swing components do not work well when using a screenreader (and the Java AccessBridge for Windows). Know that different screenreaders result in different behaviour. And in my experience the SWT-Tree performs a lot better than the Swing-Tree in combination with a screenreader. Thus our application ended up in using both SWT and Swing components.
For distributing and loading the proper SWT-library, you might find this link usefull: http://www.chrisnewland.com/select-correct-swt-jar-for-your-os-and-jvm-at-runtime-191
If you plan to build a full functional applications with more than a handful of features, I will suggest to jump right to using Eclipse RCP as the framework.
If your application won't grow too big or your requirements are just too unique to be handled by a normal business framework, you can safely jump with Swing.
At the end of the day, I'd suggest you to try both technologies to find the one suit you better. Like Netbeans vs Eclipse vs IntelliJ, there is no the absolute correct answer here and both frameworks have their own drawbacks.
Pro Swing:
- more experts
- more Java-like (almost no public field, no need to dispose on resource)
Pro SWT:
- more OS native
- faster
SWT was created as a response to the sluggishness of Swing around the turn of the century. Now that the differences in performance are becoming negligable, I think Swing is a better option for your standard applications. SWT/Eclipse has a nice framework which helps with a lot of boiler plate code.
精彩评论