158

I'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.

janpio
  • 10,645
  • 16
  • 64
  • 107
  • Tough question. :-) I'd go with Swing. But, have no PRO or CONs for that decision. – Pablo Santa Cruz Feb 21 '10 at 14:43
  • duplicate Q. please look for Swing vs. SWT Q's asked already on SO. FWIW, I use Swing just because I learned that way. There are native look-and-feel libraries (see jgoodies looks) – Jason S Feb 21 '10 at 14:46
  • "build a tool that automates a very repetitive task in a web application" - any info on this? There may be an existing tool - and I question the need for a desktop app to automate this - it may work in your case right now - but what if you move to a hosted solution? – Nate Feb 21 '10 at 14:52
  • You don’t need to learn a GUI framework for a desktop app. If you can use html css and js (which I am assuming you are) you can use Electron to build native looking apps with web languages. – Pranav A. Sep 14 '18 at 17:36
  • Electron was invented a few years after I asked this question ;) But of course today you are correct. – janpio Sep 14 '18 at 17:49

10 Answers10

153

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.
xav
  • 5,452
  • 7
  • 48
  • 57
josefx
  • 15,506
  • 6
  • 38
  • 63
  • 34
    Swing will be closer to "write once, run anywhere". SWT will be more like "write once, tweak/test everywhere". But this same discussion happened with other languages as well. – Mark Feb 23 '10 at 22:06
  • 13
    Realistically, the "Native" look and feel of Swing behaves considerably differently from my Gnome desktop -- while, for some reason, the themes work quite well, the menus look awful and are almost unusable. – Hut8 Dec 09 '10 at 07:25
  • 10
    Starting with Eclipse 3.7, VEP was replaced by WindowBuilder (which also supports both Swing and SWT). – Alexey Romanov Oct 14 '11 at 08:28
  • 7
    SWT's advantage is also less memory consuption because of native components. This should be desirable on machines with limited memory and memory differences between swing and swt can be big in large GUI designs. – jantobola Nov 09 '13 at 18:20
  • If you want a native look on Swing, you have to change the Look and Feel, which is by default `CrossPlatformLookAndFeel` (Metal). You have to change it to `SystemLookAndFeel` – Pranav A. Jan 18 '18 at 04:06
  • 1
    @JanTobola It's plain wrong. Native components use memory allocated on the native heap, not only on the Java heap. I've worked on large GUIs using Netbeans Platform, Eclipse RCP, SWT and Swing.There were some serious concerns of memory footprint in Swing in very early versions of Java (when it was a third party library and after from 1.1 to 1.2 too?) but it's no longer true and it's up to the developers to release lots of resources in SWT, there are much more opportunities of memory leak with SWT whereas an unreferenced component ends up by being "disposed" with Swing. – gouessej Feb 01 '18 at 09:54
  • @josefx JOGL 2 supports SWT and Swing. The build-in Eclipse SWT GLCanvas is mostly unmaintained whereas JOGL SWT GLCanvas and NewtCanvasSWT are still maintained. JOGL AWT GLCanvas, GLJPanel and NewtCanvasAWT work very well. – gouessej Feb 01 '18 at 09:57
  • @PranavAvva However the system L&F does not actually work like the system GUI does. It looks kinda cheap, trying to imitate the system but with little success. – std4453 Apr 10 '18 at 10:52
64

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.

Ludovico Fischer
  • 1,602
  • 11
  • 11
  • 2
    Interesting point. As a user I'm still staggered why this is such a big deal. But well, it is so I will have to consider this. Thanks. – janpio Mar 03 '10 at 18:19
  • btw: javaws (webstart) isn't available for 64 IMHO – Karussell Dec 26 '10 at 14:35
  • 1
    @Karussell: As of 3/4/2011, Sun's 64-bit JVM for Windows has JNLP support. I think that's been true for a while, but I'm not sure how long. – The Alchemist Mar 08 '11 at 05:03
23

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

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • 1
    Yes NetBeans has Matisse as a GUI builder, but the code is really verbose, confusing to read, and next to impossible to edit by source code. If you really want a GUI builder go with eclipses WindowBuilder – Pranav A. Sep 14 '18 at 17:34
14

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.

clartaq
  • 5,320
  • 3
  • 39
  • 49
10

I whould choose swing just because it's "native" for java.

Plus, have a look at http://swingx.java.net/.

Jonas Söderström
  • 4,856
  • 2
  • 36
  • 45
zeroed
  • 1,081
  • 1
  • 9
  • 18
8

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.

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
6

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)... :-)

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
5

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

incomudro
  • 548
  • 4
  • 12
3

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.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
3

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
nanda
  • 24,458
  • 13
  • 71
  • 90
  • 10
    I think the "faster" point is highly contentious. – Russ Hayward Feb 21 '10 at 17:47
  • SWT is cumbersome to use, I had to test my GUI with each version of Windows, some bugs were reproducible only under Windows Vista. Some methods are simply unimplemented or call AWT under the hood which means that you can't use a compact JRE without AWT and Swing without risking to break SWT. I started using SWT in 2009 and in my humble opinion, it's not faster. I advise you to provide a carefully designed benchmark. – gouessej Feb 01 '18 at 10:04
  • @gouessej You need AWT and Swing to run Swing, too ... – SamB Mar 08 '22 at 01:40
  • @SamB And so what? – gouessej Mar 09 '22 at 11:30