<更新記録>
2008年 1月 22日
執筆

姉妹サイト検索 Web検索


ShellEvent

トップレベルウィンドウが開いた、閉じた、等のイベントは、 ShellListenerを通して ShellEventとしてキャッチします。 このリスナは、Shell.addShellListener()によって付加することができます。

サンプル

ShellListenerTest.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ShellListenerTest {

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		
		final Button button = new Button(shell, SWT.PUSH);
		button.setText("終了");
		button.pack();
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// ボタンが押されたことを示すフラグをセット。
				button.setData(new Boolean(true));
				// シェルを閉じる。
				shell.close();
			}
		});
		
		shell.addShellListener(new ShellAdapter() {
			/* 
			 * ウィンドウが現れた時に発生。
			 * (プログラム開始時、最小化解除時、
			 * 他のアプリケーションからフォーカスが移った時。)
			 */
			public void shellActivated(ShellEvent e) {
				System.out.println("activated");
			}
			/*
			 * ウィンドウが隠れた時に発生。
			 * (最小化時、他のアプリケーションからフォーカスが移った時。)
			 * shell.dispose()では呼び出されない。
			 */
			public void shellDeactivated(ShellEvent e) {
				System.out.println("deactivated");
			}
			/*
			 * ウィンドウ最小化時に発生。
			 */
			public void shellIconified(ShellEvent e) {
				System.out.println("iconified");
			}
			/*
			 * ウィンドウ最小化解除時に発生。
			 */
			public void shellDeiconified(ShellEvent e) {
				System.out.println("deiconified");
			}
			/*
			 * shell.disposed()が呼ばれた時、
			 * ウィンドウの閉じるボタンクリック時、
			 * Alt+F4押下時に発生。
			 */
			public void shellClosed(ShellEvent e) {
				System.out.println("closed");
				// ボタンが押されたことを示すフラグをチェック。
				if (new Boolean(true).equals(button.getData())) {
					e.doit = true;
				} else {
					e.doit = false;
				}
			}
		});
		
		shell.pack();
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
}
次のようにウィンドウを操作してみました。
プログラム実行
↓
最小化
↓
最小化解除
↓
[Alt]+[F4] (e.doit = falseとするのでウィンドウは閉じない)
↓
終了ボタン押下 (e.doit = trueとするのでウィンドウは閉じる)
activated
iconified
deactivated
deiconified
activated
closed
closed


Powered by VeryEasyCMS