SWTで、複数画面の表示を実現したいです。

下記URLのようにやれば、一応は出来るのですが、タスクバーのところに表示したウィンドウ分、同じプログラムのものが表示されてしまいます。
複数のウィンドウを表示させても、タスクバーには一つしか表示させない、ということは可能でしょうか?

http://blog.goo.ne.jp/xmldtp/e/fb0d5b4fcbab43d4bbeed5ce0f56b759

回答の条件
  • 1人2回まで
  • 登録:
  • 終了:2011/05/14 15:35:03
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

回答2件)

id:hanabc No.1

回答回数20ベストアンサー獲得回数4

ポイント35pt

メインウィンドウ+複数のモードレスダイアログで実現してはどうでしょうか。

簡単なサンプルソースです。この例ではボタンを押すたびにウィンドウっぽいダイアログが開きます。

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SampleDialog extends Dialog {

	public SampleDialog(Shell parentShell) {
		super(parentShell);
	}

	public void open() {
		Shell parent = getParent();
		Shell shell = new Shell(parent, SWT.DIALOG_TRIM);
		shell.setText(getText());
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public static void main(String[] args) {
		Display display = new Display();

		final Shell shell = new Shell(display);
		shell.setText("Main");
		shell.setLayout(new RowLayout(SWT.VERTICAL));

		Button button1 = new Button(shell, SWT.BORDER);
		button1.setText("ウィンドウ");
		button1.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				SampleDialog dialog = new SampleDialog(shell);
				dialog.setText("SampleDlg");
				dialog.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

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

}
id:rapuntuleru

回答ありがとうございます。

モードレスダイアログは知りませんでした。

上記のコードだと、常にダイアログの方が前面になると思いますが、メインのウィンドウの方も選択すれば前面に出せるようにはならないでしょうか?

2011/05/08 12:49:43
id:hanabc No.2

回答回数20ベストアンサー獲得回数4

ポイント35pt

メインを選択して前に出せるように修正しました。

今度は表示されないウィンドウ(invisible)を新たに作ってそれをダイアログの親にしています。

メインウィンドウの終了時に表示されないウィンドウも終了するようにしています。

package jp.example.swt1;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SampleDialog extends Dialog {

	public SampleDialog(Shell parentShell) {
		super(parentShell);
	}

	public void open() {
		Shell parent = getParent();
		Shell shell = new Shell(parent, SWT.DIALOG_TRIM);
		shell.setText(getText());
		shell.open();
		Display display = parent.getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public static void main(String[] args) {
		Display display = new Display();

		final Shell shell = new Shell(display);
		shell.setText("Main");
		shell.setLayout(new RowLayout(SWT.VERTICAL));

		// 表示されないウィンドウ
		final Shell invisible = new Shell(display);
		invisible.setText("Invisible");
		invisible.setLayout(new RowLayout(SWT.VERTICAL));

		Button button1 = new Button(shell, SWT.BORDER);
		button1.setText("ウィンドウ");
		button1.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				// 表示されないウィンドウを親として作成
				SampleDialog dialog = new SampleDialog(invisible);
				dialog.setText("SampleDlg");
				dialog.open();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		// メインウィンドウが終わると同時に表示されないウィンドウも終了
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				invisible.dispose();
			}
		});

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

}

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません