Feb 262013
 

Try Icon java SWT 

Assalamu’ alaikum wr wb

Beberapa waktu lama saya menulis tentang komponent untuk membuat report pada swt http://sugik.do-event.com/?p=656, Kali ini saya akan berbagi bagaimana membuat try icon pada aplikasi java swt, Langsung saja berikut souce nya

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = new Display();
		final Shell shlSmsSender = new Shell(display);
		shlSmsSender.setText("SMS Sender");
		Image image = new Image(display, 16, 16);
		final Tray tray = display.getSystemTray();
		if (tray == null) {
			System.out.println("The system tray is not available");
		} else {
			final TrayItem item = new TrayItem(tray, SWT.NONE);
			item.setToolTipText("SWT TrayItem");
			item.addListener(SWT.Show, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("show");
				}
			});
			item.addListener(SWT.Hide, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("hide");
				}
			});
			item.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("selection");
				}
			});
			item.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("default selection");
				}
			});

			final Menu menu = new Menu(shlSmsSender, SWT.POP_UP);

			MenuItem misend = new MenuItem(menu, SWT.PUSH);
			misend.setText("Kirim Manual");
			misend.setImage(new Image(display, "icon/send.png"));
			misend.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("selection " + event.widget);
				}
			});

			MenuItem miLog = new MenuItem(menu, SWT.PUSH);
			miLog.setText("Tampilkan Log");
			miLog.setImage(new Image(display, "icon/log.png"));
			miLog.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.out.println("selection " + event.widget);
				}
			});

			MenuItem miexit = new MenuItem(menu, SWT.PUSH);
			miexit.setText("Keluar");
			miexit.setImage(new Image(display, "icon/exit.png"));
			miexit.addListener(SWT.Selection, new Listener() {
				public void handleEvent(Event event) {
					System.exit(0);
				}
			});

			menu.setDefaultItem(misend);

			item.addListener(SWT.MenuDetect, new Listener() {
				public void handleEvent(Event event) {
					menu.setVisible(true);
				}
			});
			item.setImage(new Image(display, "icon/hp.png"));
		}
		// shell.setBounds(50, 50, 300, 200);
		// shell.open();
		while (!shlSmsSender.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		image.dispose();
		display.dispose();
	}
}

Untuk menambahkan icon anda buat folder icon di project anda dan taruh file gambar di sana.

oke selamat mencoba semoga membantu

Wassalamu’ alaikum wr wb

Facebook Comments

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)