Habrahabr has very few articles about
SWT , so I will try to correct this small omission.
From this article you will learn:
- What is the motivation to use SWT as opposed to the main competitor - Swing?
- The main difficulties I encountered when developing a simple notebook on a bunch of Java + SWT and Some code
- How to pack and distribute your application for multiple platforms
If you are interested in - I ask under the cat.
Preamble
As you know, Java is famous for its cross-platform (write-once - run anywhere), and this also applies to GUI applications. Although, perhaps, Java and not the most common platform for writing GUI applications. And for a number of reasons.
Perhaps the main reason for this is the conviction that code written for a particular platform will work faster than code written for something abstract in a vacuum. Well, in my opinion perhaps this is true.
')
Some will disagree with me and say that this is exactly the style of Java - written once must be executed and look the same on all platforms. And
Swing is a good confirmation. Of course, the Swing application can be customized using various themes (Look And Feel), but still it will feel and look not quite like its own.
Although the guys from JetBrains were able to prove (at least to me personally) that Swing applications can be very, even friendly, but the fact remains - it begins to slow down over time, not because it is written poorly, but because of the fact that The GUI framework is used by Swing, which is somewhat worse with operating systems than their native components.
SWT has just approached this problem from the other side. By and large, SWT is a thin layer between a Java programmer and the native APIs of a particular operating system, which is somewhat worse than the Java ideology. But, as they say, the rules just exist in order to break them. Especially if it is good. And this in my opinion is the main motivation, why you should pay attention to SWT.
The first steps
I think that it would be clearer to the reader, I should tell you what I wanted to achieve from my notebook.
The idea was quite simple:
I wanted to make a semblance of a classic notebook, what we see under Windows. Only with tabs.
Looking ahead, I’ll say that I didn’t fully implement my idea - there is no possibility to send a document to a printer for printing, the ability to change the font, search the document. But, what happened, allowed to feel the "taste of SWT", which looked pretty pleasant.
Well then. So far there have been only a lot of words, but no action. It's time to bring a little code.
The entry point to the application is (Bootstrap.java):
final Display display = new Display(); final Shell shell = new Shell(display); final DocumentAndTabManager documentAndTabManager = new DocumentAndTabManager(shell); final TextEditor textEditor = new TextEditor(shell, documentAndTabManager); textEditor.init(); textEditor.run();
What we have done here:
- Created the display (Display), said that the shell (Shell) would use our display. (In my opinion, SWT has a fairly well-formed ideology - the application should not have more than one display object.)
- Shell (Shell) - where the main action will take place. If we draw an analogy with Swing, then we can say that this is to some extent an analogue of JPanel.
- We made an injection from the shell to our manager of documents and the text editor, so that they would not forget why they are needed and who their owners are.
Initialized shell (TextEditor.java):
shell.setLayout(new FillLayout());
In brief, the process of creating a menu:
And run our application:
shell.open();
Next, during the execution of our notebook, some of the listeners will work (in response to some kind of our action), from which we will do what we need, let's say we run some task in synchronous or asynchronous mode:
shell.getDisplay().asyncExec(new ReloadJob(documentAndTabManager, currentTab));
Packaging
Another portion of the pseudo-difficulties I encountered:
For each operating system and architecture you need to make your build. This requires jar'y for each specific platform. I used to use Maven, but for these purposes it is not very well suited, because I did not find fresh versions of SWT in the Maven repositories, and I had to manually download packages for each of the operating system / architecture platforms. In addition, where a lot of customization is needed Maven is not particularly suitable, but good old Ant comes to the rescue. As a dependency manager, I took Ivy.
A piece of ant-script (build.xml), responsible for packaging for a specific operating system / architecture:
<jar destfile="${package.dir}/${jar.prefix}_${target.platform}-${target.architecture}-${version}.jar" basedir="${output.dir}"> <restrict> <archives> <zips> <fileset refid="classpath.files"/> <fileset dir="${basedir}/lib/swt" includes="swt-${target.platform}-${target.architecture}.jar"/> </zips> </archives> </restrict> <manifest> <attribute name="Main-Class" value="swt.texteditor.simple.Bootstrap"/> </manifest> </jar>
The size of Jar's turned out to be 2 - 2.5 MB (depending on the OS and architecture), which in general is not so much.
Instead of conclusion
Having passed a small development cycle through myself I can notice
Cons SWT in my opinion:
* Few documentation / tutorials
* Google doesn't know as much about SWT as Swing
* Many problems have to be solved longer.
SWT advantages in my opinion:
* In general, simple and intuitive API
* Cross-platform native-looking application
* Out of the box faster working than the same on Swing
What did I learn from this little experiment?
SWT with all its charms and flaws impressed me pleasantly. The next time I decide to write a desktop application, I will no doubt choose a combination of Java + SWT.
PS A few screenshots in the end.
From Linux:
Native scrolling for Ubuntu (Unity)

Native menu for Ubuntu (Unity)

And from under Windows:

Under Mac OS, unfortunately there are no screenshots in the absence of Mac OS X.Kindly provided screenshot by
seneast user:

Source codes are available here:
code.google.com/p/swt-text-editor/source/browseDownload my notebook variation for your OS here:
code.google.com/p/swt-text-editor/downloads/listFrom under Windows it is launched by double click, from under Mac OS X java -XstartOnFirstThread -jar simple_edit *., Linux with the command java -jar simple_edit *. Runs under Java 1.6 and up.
Short instructions on how to start messing around with code from IDE (in English)
code.google.com/p/swt-text-editor/wiki/SourceCodeImportI will be glad to any comments, complaints and advice.