System Tray Icon

2007.03.01 23:14 from 예전글들

Many operating systems have the concept of a 'system tray', and it's often nice to be able to ofter a 'tray icon' feature for your application. SWT ships with tray icon support in the form of the org.eclipse.swt.widgets.TrayIcon class.

Using a tray icon is fairly simple in SWT:

image = new Image(display, 
BalloonExample.class.getResourceAsStream("tray_icon.gif")); // ... Tray tray = display.getSystemTray(); if(tray != null) { TrayItem trayItem = new TrayItem(tray, SWT.NONE); trayItem.setImage(image); }

The null check is required simply because not all platforms have a tray, and as such it's an optional feature.

Here is a screenshot of the tray icon on my Ubuntu Linux installation

사용자 삽입 이미지

Most tray items have some kind of menu associated with them. Adding a menu is also quite straightforward.

Tray tray = display.getSystemTray();
if(tray != null) {
	TrayItem item = new TrayItem(tray, SWT.NONE);
	item.setImage(image);final Menu menu = new Menu(shell, SWT.POP_UP);
	MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button A");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button B");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Show Tooltip");
	item.addListener (SWT.MenuDetect, new Listener () {
		public void handleEvent (Event event) {
			menu.setVisible (true);
		}
	});
}
사용자 삽입 이미지

Recent releases of SWT (post-M5) finally support balloons/tooltips through the tray icon. Balloons are quite common in platforms with tray icons, so this is a nice feature to finally have. To add a tooltip, simply use the TrayItem.setToolTip(ToolTip) method, and set the visibility of the tooltip to true when you want to show it.

final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setMessage("Balloon Message Goes Here!");
Tray tray = display.getSystemTray();
if (tray != null) {
	TrayItem item = new TrayItem(tray, SWT.NONE);
	item.setImage(image);
	tip.setText("Balloon Title goes here.");
	item.setToolTip(tip);
	final Menu menu = new Menu(shell, SWT.POP_UP);
	MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button A");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button B");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Show Tooltip");
	// Add tooltip visibility to menu item.
	menuItem.addListener (SWT.Selection, new Listener () {			
		public void handleEvent (Event e) {
			tip.setVisible(true);
		}
	});
	// Add menu detection listener to tray icon.
	item.addListener (SWT.MenuDetect, new Listener () {
		public void handleEvent (Event event) {
			menu.setVisible (true);
		}
	});
}
사용자 삽입 이미지

One of the suggestions made in the SWT snippets is to still show the tooltip even if tray support isn't available (typically, tooltips are triggered by some application behavior). To do this, the tooltip has to be placed manually, rather than as a relative of the tray icon.

final ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setMessage("Balloon Message Goes Here!");
tip.setText("Balloon Title goes here.");
Tray tray = display.getSystemTray();
if (tray != null) {
	TrayItem item = new TrayItem(tray, SWT.NONE);
	item.setImage(image);			
	item.setToolTip(tip);
	final Menu menu = new Menu(shell, SWT.POP_UP);
	MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button A");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Button B");
	menuItem = new MenuItem(menu, SWT.PUSH);
	menuItem.setText("Show Tooltip");
	menuItem.addListener (SWT.Selection, new Listener () {
		public void handleEvent (Event e) {
			tip.setVisible(true);
		}
	});
	item.addListener (SWT.MenuDetect, new Listener () {
		public void handleEvent (Event event) {
			menu.setVisible (true);
		}
	});
}
else {			
	// Set the tooltip location manually.
	tip.setLocation(100, 100);
}

출처 : http://www.eclipsezone.com/eclipse/forums/t66093.rhtml
퍼가실때에는 출처표시를 반드시 하셔야 합니다. 출처: http://zyint.tistory.com/

Posted by 진트­ 트랙백 0 : 댓글 0

댓글을 달아 주세요

SWT Table Simple Demo

2007.02.26 14:37 from 예전글들
사용자 삽입 이미지

import
org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

public class TableClass {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(280, 300);
    shell.setText("Table Example");

    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(25, 240, 220, 25);

    Table table = new Table(shell, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL
        | SWT.H_SCROLL);
    table.setHeaderVisible(true);
    String[] titles = { "Col 1", "Col 2", "Col 3", "Col 4" };

    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
      TableColumn column = new TableColumn(table, SWT.NULL);
      column.setText(titles[loopIndex]);
    }

    for (int loopIndex = 0; loopIndex < 24; loopIndex++) {
      TableItem item = new TableItem(table, SWT.NULL);
      item.setText("Item " + loopIndex);
      item.setText(0, "Item " + loopIndex);
      item.setText(1, "Yes");
      item.setText(2, "No");
      item.setText(3, "A table item");
    }

    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
      table.getColumn(loopIndex).pack();
    }

    table.setBounds(25, 25, 220, 200);

    table.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        if (event.detail == SWT.CHECK) {
          text.setText("You checked " + event.item);
        } else {
          text.setText("You selected " + event.item);
        }
      }
    });

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


퍼가실때에는 출처표시를 반드시 하셔야 합니다. 출처: http://zyint.tistory.com/

Posted by 진트­ 트랙백 0 : 댓글 0

댓글을 달아 주세요

Shell Style

2007.02.26 13:53 from 예전글들
SWT.BORDER,
SWT.CLOSE,
SWT.MIN,
SWT.MAX,
SWT.RESIZE,
SWT.TITLE,
SWT.NO_TRIM,
SWT.SHELL_TRIM,
SWT.DIALOG_TRIM,
SWT.MODELESS,
SWT.PRIMARY_MODAL,
SWT.APPLICATION_MODAL,
SWT.SYSTEM_MODAL
SWT.ON_TOP,
SWT.TOOL,
퍼가실때에는 출처표시를 반드시 하셔야 합니다. 출처: http://zyint.tistory.com/

Posted by 진트­ 트랙백 0 : 댓글 0

댓글을 달아 주세요

사용자 삽입 이미지


/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved.
 *
 * Created on Jan 25, 2004 7:58:06 PM by JACK
 * $Id$
 *
 * visit: http://www.asprise.com/swt
 *****************************************************************************/



import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class RingShell {
 
  int[] createCircle(int radius, int centerX, int centerY) {
    int[] points = new int[360 * 2];
    for(int i=0; i<360; i++) {
      points[i*2] = centerX + (int)(radius * Math.cos(Math.toRadians(i)));
      points[i*2+1] = centerY + (int)(radius * Math.sin(Math.toRadians(i)));
    }
    return points;
  }
 
  Point originalPosition = null;

  public RingShell() {
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
   
    Region region = new Region();
    region.add(createCircle(100, 100, 100));
    region.subtract(createCircle(50, 100, 100));
    shell.setRegion(region);
   
    // Make the shell movable by using the mouse pointer.
    shell.addMouseListener(new MouseListener() {
      public void mouseDoubleClick(MouseEvent e) {
        shell.dispose(); // Double click to dispose the shell.
      }

      public void mouseDown(MouseEvent e) {
        originalPosition = new Point(e.x, e.y);
      }

      public void mouseUp(MouseEvent e) {
        originalPosition = null;
      }
    });
   
    shell.addMouseMoveListener(new MouseMoveListener() {
      public void mouseMove(MouseEvent e) {
        if(originalPosition == null)
          return;
        Point point = display.map(shell, null, e.x, e.y);
        shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
        System.out.println("Moved from: " + originalPosition + " to " + point);
      }
    });
   
    Rectangle regionBounds = region.getBounds();
    shell.setSize(regionBounds.width, regionBounds.height);
    shell.open();

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
    region.dispose();
  }
 
  public static void main(String[] args) {
    new RingShell();
  }
}




 드래그도 가능!
출처 : http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/RingShell.htm
퍼가실때에는 출처표시를 반드시 하셔야 합니다. 출처: http://zyint.tistory.com/

Posted by 진트­ 트랙백 0 : 댓글 0

댓글을 달아 주세요

사용자 삽입 이미지

/*
 * Create a non-rectangular shell to simulate transparency
 *
 * 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.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Snippet180 {

  public static void main(String[] args) {
    Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_WARNING);
    // Shell must be created with style SWT.NO_TRIM
    final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
    shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
    // define a region
    Region region = new Region();
    Rectangle pixel = new Rectangle(0, 0, 1, 1);
    for (int y = 0; y < 200; y += 2) {
      for (int x = 0; x < 200; x += 2) {
        pixel.x = x;
        pixel.y = y;
        region.add(pixel);
      }
    }
    // define the shape of the shell using setRegion
    shell.setRegion(region);
    Rectangle size = region.getBounds();
    shell.setSize(size.width, size.height);
    shell.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        Rectangle bounds = image.getBounds();
        Point size = shell.getSize();
        e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 10,
            10, size.x - 20, size.y - 20);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    region.dispose();
    display.dispose();
  }
}



출처 : http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Createanonrectangularshelltosimulatetransparency.htm
퍼가실때에는 출처표시를 반드시 하셔야 합니다. 출처: http://zyint.tistory.com/

Posted by 진트­ 트랙백 0 : 댓글 0

댓글을 달아 주세요


티스토리 툴바