Java Tutorial/SWT/Dialog
Содержание
- 1 17. Create a dialog shell
- 2 17. Create a dialog shell and position it
- 3 17. Create a dialog shell (prompt for a value)
- 4 17. Create your own dialog classes which allows users to input a String
- 5 17. Creating Your Own Dialogs
- 6 17. Display an Empty Dialog
- 7 17. Prevent escape from closing a dialog
- 8 17. Using the Dialogs
17. Create a dialog shell
<source lang="java">
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DialogShellCreate {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Shell dialog = new Shell(shell); dialog.setText("Dialog"); dialog.setSize(200, 200); dialog.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
17. Create a dialog shell and position it
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DialogShellPositionIt {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); Point pt = display.getCursorLocation(); dialog.setLocation(pt.x, pt.y); dialog.setText("Dialog Shell"); dialog.setSize(100, 100); dialog.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
17. Create a dialog shell (prompt for a value)
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Shell example snippet: create a dialog shell (prompt for a value) * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class DialogShellPromptValue {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.pack(); final boolean[] result = new boolean[1]; final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); dialog.setLayout(new RowLayout()); final Button ok = new Button(dialog, SWT.PUSH); ok.setText("OK"); Button cancel = new Button(dialog, SWT.PUSH); cancel.setText("Cancel"); Listener listener = new Listener() { public void handleEvent(Event event) { result[0] = event.widget == ok; dialog.close(); } }; ok.addListener(SWT.Selection, listener); cancel.addListener(SWT.Selection, listener); dialog.pack(); dialog.open(); System.out.println("Prompt ..."); while (!dialog.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } System.out.println("Result: " + result[0]); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
17. Create your own dialog classes which allows users to input a String
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; class InputDialog extends Dialog {
private String message; private String input; public InputDialog(Shell parent) { this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); } public InputDialog(Shell parent, int style) { super(parent, style); setText("Input Dialog"); setMessage("Please enter a value:"); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getInput() { return input; } public void setInput(String input) { this.input = input; } public String open() { Shell shell = new Shell(getParent(), getStyle()); shell.setText(getText()); createContents(shell); shell.pack(); shell.open(); Display display = getParent().getDisplay(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } return input; } private void createContents(final Shell shell) { shell.setLayout(new GridLayout(2, true)); Label label = new Label(shell, SWT.NONE); label.setText(message); GridData data = new GridData(); data.horizontalSpan = 2; label.setLayoutData(data); final Text text = new Text(shell, SWT.BORDER); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; text.setLayoutData(data); Button ok = new Button(shell, SWT.PUSH); ok.setText("OK"); data = new GridData(GridData.FILL_HORIZONTAL); ok.setLayoutData(data); ok.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { input = text.getText(); shell.close(); } }); Button cancel = new Button(shell, SWT.PUSH); cancel.setText("Cancel"); data = new GridData(GridData.FILL_HORIZONTAL); cancel.setLayoutData(data); cancel.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { input = null; shell.close(); } }); shell.setDefaultButton(ok); }
} public class CustomInputDialog {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); InputDialog dlg = new InputDialog(shell); String input = dlg.open(); if (input != null) { // User clicked OK; set the text into the label System.out.println(input); } while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
17. Creating Your Own Dialogs
To create your own dialog, you do the following:
- Subclass org.eclipse.swt.widgets.Dialog.
- Implement a method named open() that returns an object appropriate to your dialog"s purpose.
- In open(), create the window, create the controls and event handlers (including controls and event handlers to dismiss the dialog), and display the window.
- Provide getters and setters for any data.
17. Display an Empty Dialog
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class DialogEmptyDisplay {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); final Shell dialog =new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); dialog.setLayout(new RowLayout()); dialog.pack(); dialog.open(); // Move the dialog to the center of the top level shell. Rectangle shellBounds = shell.getBounds(); Point dialogSize = dialog.getSize(); dialog.setLocation( shellBounds.x + (shellBounds.width - dialogSize.x) / 2, shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
// Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
17. Prevent escape from closing a dialog
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Shell example snippet: prevent escape from closing a dialog * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class ShellEscapeCloseDialog {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); Button b = new Button(shell, SWT.PUSH); b.setText("Open Dialog ..."); b.pack(); b.setLocation(10, 10); b.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent se) { Shell dialog = new Shell(shell, SWT.DIALOG_TRIM); dialog.addListener(SWT.Traverse, new Listener() { public void handleEvent(Event e) { if (e.detail == SWT.TRAVERSE_ESCAPE) { e.doit = false; } } }); dialog.open(); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
17. Using the Dialogs
- The common dialog classes in SWT descend from SWT"s abstract Dialog class (org.eclipse.swt.widgets.Dialog).
- A dialog"s parent, which is passed to the constructor, is always a Shell object.
ConstantDescriptionSWT.APPLICATION_MODALModal to the application; input is blocked to other windows in the application.SWT.PRIMARY_MODALModal to the parent window of the dialog; input is blocked to the parent of the dialog only.SWT.SYSTEM_MODALInput is blocked to all other windows of all applications until the dialog is dismissed.SWT.NONEModeless (the default).