Swing vs. SWT – Side-by-side comparison

In many articles, developers have compared Swing with SWT with a lot of details. However, there is not a side-by-side comparison by using a diagram which may give a direct impression of how they are different. In this post, I will compare Swing and SWT side by side and get a taste of the difference of those two libraries. As you may know, those two libraries mainly has three aspects to compare, available widgets, layout managers, and event handling types. In addition of a side-by-side comparison I will give a simple code example at the end.

1. Basic class hierarchy Comparison.
2. Layout managers.
3. Event types.
4. Code Example.

1. Basic class hierarchy Comparison.

2. Layout managers.

Since Swing is built on AWT and provides a pluggable look and feel, Swing should have all the layout managers of AWT. This may make the problem a little more complicated than SWT. SWT is a complete system by itself and there is no need to refer to other libraries. This may be another advantage of SWT over Swing.

3. Event types.
Swing also has all the events of AWT.

4. Code Examples

The following code examples will use gridlayout from Swing and SWT, and add an action listener for a button. It can give a taste how Swing and SWT are used differently.

Swing Code Example:

public class TestSwing extends JFrame {
 
	public TestSwing() {
 
		JPanel panel = new JPanel();
 
		panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
		panel.setLayout(new GridLayout(2, 2, 50, 50));
 
		final JLabel l1 = new JLabel("nothing");
 
		JButton b1 = new JButton("Start");
		b1.addActionListener(new ActionListener() {
 
			public void actionPerformed(ActionEvent ae) {
				l1.setText("Button has been clicked");
			}
 
		});
 
		panel.add(b1);
		panel.add(l1);
		panel.add(new JButton("Start"));
 
		add(panel);
 
		setTitle("Swing Example");
		setSize(600, 400);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
 
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TestSwing ex = new TestSwing();
				ex.setVisible(true);
			}
		});
	}
 
}

SWT Code Example:

public class TestSWT {
 
	public static void main(String[] args) {
 
		Display display = new Display();
		Shell shell = new Shell(display);
 
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.makeColumnsEqualWidth = true;
 
		shell.setLayout(gridLayout);
 
		Button b1 = new Button(shell, SWT.PUSH);
		b1.setText("button 1");
		b1.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
 
		final Label l2 = new Label(shell, SWT.PUSH);
		l2.setText("button 2");
		GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
		l2.setLayoutData(gridData);
 
		Button button3 = new Button(shell, SWT.PUSH);
		button3.setText("button 3");
 
		button3.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				l2.setText("clicked");
			}
 
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub
				l2.setText("clicked");
			}
		});
 
		button3.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
 
		shell.pack();
		shell.open();
	}
}

As Eclipse is a proven successful framework for extensible software development, SWT is a good choice since they are more compatible with each other.

Category >> GUI >> Versus