AWT Layout Managers for SWT

Description

I've converted three of the main AWT layout managers for use in SWT. You can use these layouts inside Eclipse, or in a standalone SWT application.

For now, the docs are light; I just wanted to get this up for people to try...

Installation

To install the SWT/AWT layout managers in eclipse.

When mentioned, installdir refers to the directory in which you installed Eclipse. This is often c:\eclipse, but could be any directory you chose.

  1. Shutdown eclipse if it's running.
  2. Download com.javadude.swt.layouts_1.0.0.zip from this site. This zip includes the executables for the layouts. I'll provide source in the future, once I clean it up ;)
  3. Extract the zip file into the installdir directory. This should add a com.javadude.swt.layouts directory into your installdir/plugins directory.
  4. Startup Eclipse. The plugin is loaded and should be available for use in your plugins or applications.

Use

First, create a plugin project that you want to use the SWT/AWT layouts in. Add the "JavaDude SWT/AWT Layouts Plugin" as a dependency to your plugin.

When writing your code, you use the layouts just like any other SWT layout. Borderlayout requires that you set layout constraints on the contained components. Some examples (more detailed docs and examples with appear later...)

BorderLayout Example

Figure 1: BorderLayout Example

Composite parent = ...;



parent.setLayout(new BorderLayout());

  // Make sure you choose com.javadude.swt.layouts.BorderLayout, not java.awt.BorderLayout!!!



Button northButton = new Button(parent, SWT.DEFAULT);

northButton.setLayoutData(BorderLayout.NORTH);

northButton.setText("North");



Button southButton = new Button(parent, SWT.DEFAULT);

southButton.setLayoutData(BorderLayout.SOUTH);

southButton.setText("South");



// etc...



// You can also set the hgap and vgap in the same manner as AWT's BorderLayout

// For example:

parent.setLayout(new BorderLayout(5,3));

GridLayout Example

Figure 2: GridLayout Example

Composite parent = ...;



parent.setLayout(new GridLayout(2,0));

  // Make sure you choose com.javadude.swt.layouts.GridLayout, not java.awt.GridLayout!!!



Button b1 = new Button(parent, SWT.DEFAULT);

b1.setText("1");



Button b2 = new Button(parent, SWT.DEFAULT);

b2.setText("2");



// etc...



// You can also set the hgap and vgap in the same manner as AWT's GridLayout

// For example:

parent.setLayout(new GridLayout(2,0,5,3));

FlowLayout Example

Figure 3: FlowLayout example

Composite parent = ...;



parent.setLayout(new FlowLayout());

  // Make sure you choose com.javadude.swt.layouts.FlowLayout, not java.awt.FlowLayout!!!



Button b1 = new Button(parent, SWT.DEFAULT);

b1.setText("1");



Button b2 = new Button(parent, SWT.DEFAULT);

b2.setText("2");



// etc...



// You can also set the hgap, vgap and alignment in the same manner as AWT's FlowLayout

// For example:

parent.setLayout(new FlowLayout(FlowLayout.RIGHT));

Uninstalling from Eclipse

To uninstall from Eclipse, simply delete the installdir/plugins/com.javadude.swt.layouts directory.

License

This AWT/SWT layouts package is free for any use other than selling it as a stand-alone product.

Note that this code is provided "as-is" without any warrantee or guarantee of suitability for any purpose. Scott Stanchfield cannot be held responsible for any damage caused by use this code.

(In other words, use 'em for free at your own risk.)

Bug Reports/Feature Requests

Please let me know about any bugs you find or requests for what you'd like to see this tool do.  Feel free to send example code to implement it as well.

Future Directions

  • I need to document and comment the code like crazy
  • Providing source
  • Providing more complete examples