Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] SWT 101

Hey, who knows, it may just be that I need some basic SWT 101 training.  Let
me post a simple source.  It creates a panel with one entry field and one
"output" field.  The output field is a label, as are two other constants on
the panel.  There are two buttons at the bottom, Find and Exit.  You type
something in the entry field and press Find, and something else is placed in
the output field.  CAT, DOG and OCTOPUS are valid values and will return a
description, anything else returns the value "Not Found" and causes the
input and output to be colored red.

Nothing to it.

Except that if you run the code as it stands, the output label never gets
updated unless you resize the window.  The only way I've found to correct
this behavior is to either call pack() every time (which is really ugly - go
ahead, try it!  Watch what happens to the input field), or to use the
commented line which initializes the label to a bunch of blanks (thereby
forcing some default width perhaps?).

Joe


SOURCE:

import java.util.Hashtable;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Prompt {

  // Resources
  Display display;
  Shell shell;
  Color red, white;

  // Fields
  Text f1;
  Label f2;

  public Prompt() {
  }

  public void run() {
    init();
    setLayout();
    createWidgets();
    show();
    cleanup();
  }

  private void init() {
    // Create a standard window
    display = new Display();
    shell = new Shell(display);
    shell.setText("Prompt");

    // Create some colors
    red = new Color(display, 255, 0, 0);
    white = new Color(display, 255, 255, 255);
  }

  private void setLayout() {
    // Create the layout for the widgets
    GridLayout grid = new GridLayout();
    grid.numColumns = 2;
    grid.makeColumnsEqualWidth = true;
    shell.setLayout(grid);
  }

  private void createWidgets() {
    // Create my widgets
    Label l1 = new Label(shell, SWT.NONE);
    l1.setText("Item Number:");

    f1 = new Text(shell, SWT.BORDER | SWT.SINGLE);
    f1.setTextLimit(20);

    Label l2 = new Label(shell, SWT.NONE);
    l2.setText("Description:");

    f2 = new Label(shell, SWT.NONE);
//  f2.setText("                    ");

    Button b1 = new Button(shell, SWT.PUSH);
    b1.setText("Find");
    b1.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        doFind();
      }
    });

    Button b2 = new Button(shell, SWT.PUSH);
    b2.setText("Exit");
    b2.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        doExit();
      }
    });
  }

  private void doFind() {
    String desc = getDescription(f1.getText());
    if (desc == null)
    {
      f1.setForeground(white);
      f1.setBackground(red);
      f2.setText("Not Found");
      f2.setForeground(red);
    }
    else
    {
      f1.setForeground(null);
      f1.setBackground(null);
      f2.setText(desc);
      f2.setForeground(null);
    }
    f1.update();
    f2.update();
  }

  private void doExit() {
    shell.close();
  }

  private static final Hashtable items;
  static {
    items = new Hashtable();
    items.put("DOG", "My Puppy");
    items.put("CAT", "My Kitty");
    items.put("OCTOPUS", "Calamari Kid");
  }

  private String getDescription(String item)
  {
    return (String) items.get(item);
  }

  private void show() {
    // Final setup - size the display show it
    shell.setSize(200, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
  }

  private void cleanup() {
    // When done, clean up resources
    display.dispose();
    red.dispose();
    white.dispose();
  }

  public static void main(String[] args) {
    new Prompt().run();
    System.exit(0);
  }
}



Back to the top