Java/SWT JFace Eclipse/Label

Материал из Java эксперт
Перейти к: навигация, поиск

CLabel Border

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 26, 2004 11:02:38 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelBorder {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  Image image = new Image(display, "jexp.gif");
  public CLabelBorder() {
    
    CLabel label1 = new CLabel(shell, SWT.SHADOW_OUT);
    label1.setText("SHADOW_OUT");
    label1.setImage(image);
    label1.setBounds(10, 10, 150, 100);
    
    CLabel label2 = new CLabel(shell, SWT.SHADOW_IN);
    label2.setText("SHADOW_IN");
    label2.setImage(image);
    label2.setBounds(170, 10, 150, 100);
    label2.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_GREEN), 
                       display.getSystemColor(SWT.COLOR_RED),
                       display.getSystemColor(SWT.COLOR_BLUE), 
                       display.getSystemColor(SWT.COLOR_WHITE)},
                 new int[] {25, 50, 100});

    CLabel label3 = new CLabel(shell, SWT.SHADOW_NONE);
    label3.setText("SHADOW_NONE");
    label3.setBackground(image);    
    label3.setBounds(330, 10, 150, 100);
    
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new CLabelBorder();
  }
}





CLabel Gradient Background

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 26, 2004 11:31:38 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CLabelGradientBG {
  Display display = new Display();
  Shell shell = new Shell(display);
  Image image = new Image(display, "jexp.gif");
  public CLabelGradientBG() {
    init();
    CLabel labelGradientBg = new CLabel(shell, SWT.SHADOW_IN);
    labelGradientBg.setText("CLabel with gradient colored background");
    labelGradientBg.setImage(image);
    labelGradientBg.setBounds(10, 10, 300, 100);
    labelGradientBg.setBackground(
      new Color[] {
        display.getSystemColor(SWT.COLOR_GREEN),
        display.getSystemColor(SWT.COLOR_WHITE),
        display.getSystemColor(SWT.COLOR_RED)},
      new int[] { 50, 100 });
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new CLabelGradientBG();
  }
}





Create a label (a separator)

/*
 * Label example snippet: create a label (a separator)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class Snippet37 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Create a label (with an image)

/*
 * Label example snippet: create a label (with an image)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class Snippet34 {
  public static void main(String[] args) {
    Display display = new Display();
    Image image = new Image(display, 16, 16);
    Color color = display.getSystemColor(SWT.COLOR_RED);
    GC gc = new GC(image);
    gc.setBackground(color);
    gc.fillRectangle(image.getBounds());
    gc.dispose();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.BORDER);
    label.setImage(image);
    label.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    image.dispose();
    display.dispose();
  }
}





Demonstrates CLabel

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates CLabel
 */
public class CLabelShort {
  private Image lookImage;
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Short");
    // Load the image
    lookImage = new Image(display, this.getClass().getResourceAsStream(
        "jexp.gif"));
    createContents(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    // Dispose the image
    if (lookImage != null) lookImage.dispose();
    display.dispose();
  }
  /**
   * Creates the main window"s contents
   * 
   * @param parent the main window
   */
  private void createContents(Composite parent) {
    parent.setLayout(new FillLayout());
    // Create the CLabel
    CLabel label = new CLabel(parent, SWT.LEFT);
    label.setText("This is a CLabel with a lot of long-winded text");
    label.setImage(lookImage);
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new CLabelShort().run();
  }
}





Demonstrates CLabel 2

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates CLabel
 */
public class CLabelTest {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Test");
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  /**
   * Creates the main window"s contents
   * 
   * @param parent the main window
   */
  private void createContents(Composite parent) {
    parent.setLayout(new GridLayout(1, false));
    // Create the CLabels
    CLabel left = new CLabel(parent, SWT.LEFT | SWT.SHADOW_IN);
    left.setText("Left and Shadow In");
    left.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    CLabel center = new CLabel(parent, SWT.CENTER | SWT.SHADOW_OUT);
    center.setText("Center and Shadow Out");
    center.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    CLabel right = new CLabel(parent, SWT.RIGHT | SWT.SHADOW_NONE);
    right.setText("Right and Shadow None");
    right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new CLabelTest().run();
  }
}





Demonstrates CLabel gradients

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates CLabel gradients
 */
public class CLabelGradient {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CLabel Gradient");
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  /**
   * Creates the main window"s contents
   * 
   * @param parent the main window
   */
  private void createContents(Composite parent) {
    parent.setLayout(new GridLayout(1, false));
    // Create the CLabels
    
    
    CLabel one = new CLabel(parent, SWT.LEFT);
    one.setText("First Gradient Example");
    one.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    one.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    // Set the background gradient
    one.setBackground(new Color[] {
        parent.getDisplay().getSystemColor(SWT.COLOR_RED),
        parent.getDisplay().getSystemColor(SWT.COLOR_GREEN),
        parent.getDisplay().getSystemColor(SWT.COLOR_BLUE)}, new int[] { 25, 50});
    CLabel two = new CLabel(parent, SWT.LEFT);
    two.setText("Second Gradient Example");
    two.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Set the background gradient
    two.setBackground(new Color[] {
        parent.getDisplay().getSystemColor(SWT.COLOR_WHITE),
        parent.getDisplay().getSystemColor(SWT.COLOR_GRAY),
        parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY),
        parent.getDisplay().getSystemColor(SWT.COLOR_BLACK)}, new int[] { 33, 67,
        100});
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new CLabelGradient().run();
  }
}





Demonstrates Labels

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;
/**
 * This class demonstrates Labels
 */
public class LabelExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell();
    shell.setLayout(new GridLayout(1, false));
    // Create a label
    new Label(shell, SWT.NONE).setText("This is a plain label.");
    // Create a vertical separator
    new Label(shell, SWT.SEPARATOR);
    // Create a label with a border
    new Label(shell, SWT.BORDER).setText("This is a label with a border.");
    // Create a horizontal separator
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // Create a label with an image
    Image image = new Image(display, "jexp.gif");
    Label imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(image);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





First SWT: Label

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class FirstSWTClass {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("First SWT Application");
    shell.setSize(250, 250);
    Label label = new Label(shell, SWT.CENTER);
    label.setText("Greetings from SWT");
    label.setBounds(shell.getClientArea());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Formatted Text

/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on Mar 16, 2004 7:20:52 PM by JACK
 * $Id$
 * 
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class FormattedText {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  // the label used to display the text.
  Label label;
  
  Button buttonColor;
  Button buttonFont;
  
  // current selected color.
  Color color;
  // current selected font.
  Font font;
  public FormattedText() {
    label = new Label(shell, SWT.BORDER | SWT.WRAP);
    label.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
    label.setText("Java UI Programming with SWT/JFace");
    
    buttonColor = new Button(shell, SWT.PUSH);
    buttonColor.setText("Change color");
    buttonColor.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        ColorDialog colorDialog = new ColorDialog(shell);
        if(color != null)
          colorDialog.setRGB(color.getRGB());
        RGB value = colorDialog.open();
        if(value != null) {
          if(color != null)
            color.dispose();
          color = new Color(display, value);
          label.setForeground(color);
        }else{
          System.out.println("Setting foreground color action canceled.");
        }
      }
    });
    
    buttonFont = new Button(shell, SWT.PUSH);
    buttonFont.setText("Change font");
    buttonFont.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        FontDialog fontDialog = new FontDialog(shell);
        if(font != null)
          fontDialog.setFontList(font.getFontData());
        FontData fontData = fontDialog.open();
        if(fontData != null) {
          if(font != null)
            font.dispose();
          font = new Font(display, fontData);
          label.setFont(font);
        }else{
          System.out.println("Setting font action canceled.");
        }
      }
    });
    
    label.setBounds(0, 0, 300, 120);
    buttonColor.setBounds(50, 130, 90, 25);
    buttonFont.setBounds(160, 130, 90, 25);
    shell.setSize(300, 190);
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new FormattedText();
  }
}





How to resize the Label when the Shell resizes using a Layout

import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/*
 * This example builds on HelloWorld2 and demonstrates how to resize the 
 * Label when the Shell resizes using a Layout.
 */
public class HelloWorld4 {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new HelloWorld4().open(display);
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  public Shell open(Display display) {
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Label label = new Label(shell, SWT.CENTER);
    label.setText("Hello_world");
    shell.pack();
    shell.open();
    return shell;
  }
}





how to resize the Label when the Shell resizes using a Listener mechanism

import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/*
 * This example builds on HelloWorld2 and demonstrates how to resize the 
 * Label when the Shell resizes using a Listener mechanism.
 */
public class HelloWorld3 {
  
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new HelloWorld3().open(display);
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
  public Shell open(Display display) {
    final Shell shell = new Shell(display);
    final Label label = new Label(shell, SWT.CENTER);
    label.setText("Hello_world");
    label.pack();
    shell.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent e) {
        label.setBounds(shell.getClientArea());
      }
    });
    shell.pack();
    shell.open();
    return shell;
  }
}





Label Highlighting

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Oct 28, 2003 8:10:30 PM by JACK
 * $Id: LabelHighlighting.java,v 1.1 2003/12/22 12:07:54 jackwind Exp $
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class LabelHighlighting {
  Display display = new Display();
  Shell shell = new Shell(display);
  Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);
  Listener listener = new MouseEnterExitListener();
  
  public LabelHighlighting() {
    
    label.setText("Point your cursor here ...");
    label.setBounds(30, 30, 200, 30);
    
    label.addListener(SWT.MouseEnter, listener);
    label.addListener(SWT.MouseExit, listener);
    
    shell.setText("Move your cursor to test ...");
    shell.setSize(260, 120);
    shell.open();    
    
      
    while(! shell.isDisposed()) {
      if(! display.readAndDispatch()) {// If no more entries in event queue
        display.sleep();
      }
    }
    
    display.dispose();
  }
  
  class MouseEnterExitListener implements Listener {
    public void handleEvent(Event e) {
      switch (e.type) {
        case SWT.MouseEnter :
          display.syncExec(new Runnable() {
            public void run() {
              label.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
              label.setText("Cursor enters the label");
            }
          });
          
          break;
        case SWT.MouseExit :
          display.syncExec(new Runnable() {
            public void run() {
              label.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
              label.setText("Cursor leaves the label");
            }
          });
          
          break;
      }
    }
  }

  public static void main(String[] args) {
    new LabelHighlighting();
  }
}





Label Highlighting 2

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Oct 28, 2003 8:10:30 PM by JACK
 * $Id: LabelHighlighting2.java,v 1.1 2003/12/22 12:07:54 jackwind Exp $
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LabelHighlighting2 {
  Display display = new Display();
  Shell shell = new Shell(display);
  Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);
  MouseTrackListener listener = new MouseEnterExitListener();
  
  public LabelHighlighting2() {
    
    label.setText("Point your cursor here ...");
    label.setBounds(30, 30, 200, 30);
    
    label.addMouseTrackListener(listener);
    
    shell.setText("Move your cursor to test ...");
    shell.setSize(260, 120);
    shell.open();    
    
      
    while(! shell.isDisposed()) {
      if(! display.readAndDispatch()) {// If no more entries in event queue
        display.sleep();
      }
    }
    
    display.dispose();
  }
  
  class MouseEnterExitListener implements MouseTrackListener {
    public void mouseEnter(MouseEvent e) {
      display.syncExec(new Runnable() {
        public void run() {
          label.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
          label.setText("Cursor enters the label");
        }
      });
    }
    public void mouseExit(MouseEvent arg0) {
      display.syncExec(new Runnable() {
        public void run() {
          label.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
          label.setText("Cursor leaves the label");
        }
      });
    }

    public void mouseHover(MouseEvent arg0) {
      // do nothing
    }
  }

  public static void main(String[] args) {
    new LabelHighlighting2();
  }
}





Label Highlighting 3

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Oct 28, 2003 8:10:30 PM by JACK
 * $Id: LabelHighlighting3.java,v 1.1 2003/12/22 12:07:54 jackwind Exp $
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LabelHighlighting3 {
  Display display = new Display();
  Shell shell = new Shell(display);
  Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);
  MouseTrackListener listener = new MouseEnterExitListener();
  
  public LabelHighlighting3() {
    
    label.setText("Point your cursor here ...");
    label.setBounds(30, 30, 200, 30);
    
    label.addMouseTrackListener(listener);
    
    shell.setText("Move your cursor to test ...");
    shell.setSize(260, 120);
    shell.open();    
    
      
    while(! shell.isDisposed()) {
      if(! display.readAndDispatch()) {// If no more entries in event queue
        display.sleep();
      }
    }
    
    display.dispose();
  }
  
  class MouseEnterExitListener extends MouseTrackAdapter {
    public void mouseEnter(MouseEvent e) {
      display.syncExec(new Runnable() {
        public void run() {
          label.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
          label.setText("Cursor enters the label");
        }
      });
    }
    public void mouseExit(MouseEvent arg0) {
      display.syncExec(new Runnable() {
        public void run() {
          label.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
          label.setText("Cursor leaves the label");
        }
      });
    }
  }

  public static void main(String[] args) {
    new LabelHighlighting3();
  }
}





Label Separator

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 26, 2004 9:29:09 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LabelSeparator {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  Image image = new Image(display, "jexp.gif");
  public LabelSeparator() {
    init();
    // shell.setLayout(new RowLayout());
    
    Label label = new Label(shell, SWT.BORDER);
    // Label label = new Label(shell, SWT.SEPARATOR);
    label.setImage(image);
    label.setText("Label");
    label.setBounds(10, 10, 150, 150);
    
    CLabel clabel = new CLabel(shell, SWT.SHADOW_IN);
    clabel.setImage(image);
    clabel.setText("CLabel");
    clabel.setBounds(170, 10, 150, 150);
    
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new LabelSeparator();
  }
}





Label Wrap

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 26, 2004 9:39:30 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LabelWrap {

  public LabelWrap() {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    String text = "Professional Java Interfaces With SWT/JFace, by Jack Li Guojie";
    
    Label labelNoWrap = new Label(shell, SWT.BORDER);
    labelNoWrap.setText(text);
    labelNoWrap.setBounds(10, 10, 100, 100);
    
    Label labelWrap = new Label(shell, SWT.WRAP | SWT.BORDER);
    labelWrap.setText(text);
    labelWrap.setBounds(120, 10, 100, 100);
    
    
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new LabelWrap();
  }
}





SWT Label With Text

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
 * @author Steven Holzner
 */
public class LabelWithText {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(300, 200);
    Label label = new Label(shell, SWT.CENTER);
    label.setText("No worries!");
    label.setBounds(shell.getClientArea());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}