Skip to main content

GUI Design and SWT

Posted by aiqa on May 19, 2005 at 10:46 PM PDT

For some time, I have been working on the GUI of a software that will be used internally at the company I work for. I faced some problems which are mostly related with internals of GUI design, not what is seen on the screen but how to organize the code to make it easy to understand, write bugfixes and make improvements on the software that is deeply connected with the GUI.

Some of the subjects I will discuss here are related with swt and some are the hints that made my life easier as a programmer. I learned them by making mistakes and spending too much time on thinking the answer of the question "am I doing right ?". Later I recognized that I can not find the right solution by thinking. I must apply what seems to be the best solution as soon as possible to see if I get the results I wish to derive from that solution.

You may have different experiences and better solutions to the problems that I will write here. If so please share them with me.

Swing vs SWT Debate
I get bored of reading which one is better. People who have spent years with swing supports it fanatically. I can understand this. I have not used swing much enough to be a fan of it. I have started to work on SWT a few months ago, and for about a week or so I am more focused on it because I write the GUI of the software. I can not compare Swing and SWT according to my experiences but can write about SWT.

I mostly used Combo and Tree as the main widgets. (Widget is the general name of a GUI component is SWT). With Delphi I have always created an array with a Combo to store more values than the simple text of what a user sees when he expends the combo. Swt Combo can store an object. This is excellent. If it hadn`t I could create my custom widget which will be a compound of a Combo and an ArrayList. Because SWT uses native calls to operating system you can not easily write a class which extends Combo class but create a composite and add your components onto it (actually if you don`t mind your byte code not to be platform specific you can do by implementing a method which checks if the class has been extended) . This gives you the freedom that you used to get with Swing altough swing is more flexible.

SWT development will not seem to slow down because it is the primary component of Eclipse IDE which is widely used. When you code for a company this becomes the primary requisite.

SWT is easy to learn. Online API reference is hard to find but easy to understand. FormLayout is exteremly useful and once learned you can add new widgets without modifying your source code very much.

SWT commuity is getting crowded. Not crowded !

SQL Queries
Querying a table and showing the results to user view is a headache. You have to write lots of fields` name correctly, pass them to a method, create an sql string which can never be written correctly at the first try. Then read a huge resultSet. I don`t like database driven interfaces. I used to use an array of field names and use it in a loop to get as much as I can with writing as little code as possible. With Java 1.5 I started to use variable length arguments which is a revolution as generics are. I pass table name, where and order clauses as the first three string parameters then a String... fields argument which holds field names for a select or field names and values for a insert/update statement. This approach saved lots of time and make the code clear.

Creating Widgets
I wrote a class named EasyForm (extended from Composite) which makes creating widgets easy. Every swt driven class extends EasyForm. In EasyForm, there ara methods to create basic components which does not contain bussiness logic, I mean interaction with other classes except DBConnection class. DBConnection class provides permenant database connection and some helper methods for the primary classes. I like to store database methods together to keep other classes simpler and take the advantage of reusable database methods.

SWT Tree
Swt Tree is easy to use, it holds TreeItems. Every TreeItem has a text value and an object. This object makes Tree a meaningful component because holding a dummy text (text is dummy for the programmer, sure user`s only meaningful data piece it that text) means string comparisions, database queries for related data with that text. When I learned that I can hold an object, I created a class named TreeItemData which holds a text field and index fields of the db table that Tree is derived. I created TreeItemData ArrayList using generics (new with Java 1.5). (ArrayList< TreeItemData > childItems = new ArrayList< TreeItemData >();)

Imagine a layout that at the left side of the screen there is a Tree and at the right there are Combo and Text fields which show the detailed information about the record choosen from the Tree. I can also store database values of the fields in the TreeItemData object to reduce the number of database queries.

SWT Combo
Combo widget, like TreeItem, can hold an object. (Actually this property is inherited from Widget). I created a class which holds a text and a key field pair, and named it ComboData. Text is shown in the drop down menu. When an item is selected I read corrosponding key value from the ArrayList of ComboData.

Don`t Create Objects
For performance issues use same object several times. Because creating an object means allocating new heap memory it takes more time then using preallocated heap area. If you have some "choose city" combo in several user interfaces then create one in the main object and pass it`s refrence whenever needed.

For example I used "choose city" combo (which queries city table from database) in 6 different composites .Besides memory consumption it also took too much time for my program to initialize because every instance queries the database. Using one city combo saved me. Someone should say "Oh man, Java is slow" ;)

I guess there shall be some better methods for the ones I wrote here. A GUI driven software is a bit boring to write because you put one control here one there and try to guess "dummy" user responses. They find some properties hard to use then you change the way they seem or behave after decades of the first implementation of that property. Programming becames a social event where your GUI talks and your language must be clear.

My homepage is


actualy i am a beginer in java and for my project i need to ...

actualy i am a beginer in java and for my project i need to study swt..wil u plz help me to find some good text book????