下記URLのようにやれば、一応は出来るのですが、タスクバーのところに表示したウィンドウ分、同じプログラムのものが表示されてしまいます。
複数のウィンドウを表示させても、タスクバーには一つしか表示させない、ということは可能でしょうか?
http://blog.goo.ne.jp/xmldtp/e/fb0d5b4fcbab43d4bbeed5ce0f56b759
メインウィンドウ+複数のモードレスダイアログで実現してはどうでしょうか。
簡単なサンプルソースです。この例ではボタンを押すたびにウィンドウっぽいダイアログが開きます。
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(); } }
メインを選択して前に出せるように修正しました。
今度は表示されないウィンドウ(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(); } }
回答ありがとうございます。
モードレスダイアログは知りませんでした。
上記のコードだと、常にダイアログの方が前面になると思いますが、メインのウィンドウの方も選択すれば前面に出せるようにはならないでしょうか?