Home Download Change log

Java SWT widgets

Introduction

When I started to develop in Java SWT I missed some useful widgets which I knew from e. g. TclTk. I implemented two SWT widgets by myself.

Requirements

Examples

Progress bar:

import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ProgressBarDemo
{
  public static void main(String[] args)
  {
    final ProgressBar progressBar;

    // init display
    final Display display = new Display();

    // init window
    Shell shell = new Shell(display);
    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
    rowLayout.justify = true;
    shell.setLayout(rowLayout);

    // progress bar
    progressBar = new ProgressBar(shell,SWT.NONE);
    progressBar.setMinimum(0);
    progressBar.setMaximum(100);
    RowData rowData = new RowData();
    rowData.width = 300;
    progressBar.setLayoutData(rowData);

    // start progress bar update thread
    Thread thread = new Thread()
    {
      public void run()
      {
        for (int z = 0; z <= 100; z++)
        {
          final double value = (double)z;

          display.syncExec(new Runnable()
          {
            public void run()
            {
              progressBar.setSelection(value);
            }
          });
          try { Thread.sleep(50); } catch (InterruptedException exception) {}
        }
        try { Thread.sleep(1000); } catch (InterruptedException exception) {}
        System.exit(0);
      }
    };
    thread.setDaemon(true);
    thread.start();

    // main event loop
    shell.setSize(320,50);
    shell.open();

    while (!shell.isDisposed())
    {
      if (!display.readAndDispatch()) display.sleep();
    }
  }
}

Pane:

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class PaneDemo
{
  public static void main(String[] args)
  {
    Pane       pane0,pane1,pane2;
    GridLayout gridLayout;
    GridData   gridData;
    StyledText styledText;
    Button     button;

    // init display
    Display display = new Display();

    // init window
    Shell shell = new Shell(display);
    gridLayout = new GridLayout(1,true);
    shell.setLayout(gridLayout);

    // pane
    pane0 = new Pane(shell,SWT.HORIZONTAL,null);
    gridLayout = new GridLayout(1,true);
    pane0.setLayout(gridLayout);
    gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    pane0.setLayoutData(gridData);
    {
      styledText = new StyledText(pane0,SWT.BORDER);
      gridData = new GridData();
      gridData.horizontalAlignment = GridData.FILL;
      gridData.grabExcessHorizontalSpace = true;
      gridData.verticalAlignment = GridData.FILL;
      gridData.grabExcessVerticalSpace = true;
      styledText.setLayoutData(gridData);
    }

    pane1 = new Pane(shell,SWT.HORIZONTAL,pane0);
    gridLayout = new GridLayout(2,true);
    pane1.setLayout(gridLayout);
    gridData = new GridData();
    gridData.horizontalAlignment = GridData.FILL;
    gridData.grabExcessHorizontalSpace = true;
    pane1.setLayoutData(gridData);
    {
      button = new Button(pane1,SWT.PUSH);
      button.setText("Button 1");
      button.setLayoutData(gridData);

      button = new Button(pane1,SWT.PUSH);
      button.setText("Button 2");
      button.setLayoutData(gridData);
    }

    // main event loop
    shell.setSize(320,200);
    shell.open();

    while (!shell.isDisposed())
    {
      if (!display.readAndDispatch()) display.sleep();
    }
  }
}

License

The widgets are currently under GPL version 2.

Download

ProgressBar.java V0.03
Pane.java V0.04

ChangeLog

Progress bar:
2015-02-15 0.03
  * added resize listener

2009-09-15 0.02
  * fixed layout

2009-03-01 0.02
  * clean up

2008-11-27 0.01
  * initial public release
      
Pane:
2024-09-04 0.04
  * fixed pane size calculation

2022-10-08 0.03
  * fixed pane layout

2009-09-15 0.02
  * fixed layout

2009-07-27 0.01
  * initial public release
      

Back to top