- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Dialogs in Java SWT
In this part of the Java SWT tutorial, we introduce dialogs.
Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.
MessageBox
MessageBox
is a simple dialog that provides some information to the user. It can contain messages, icons, and various buttons. Icons and buttons are chosen with available MessageBox
styles. For instance, the SWT.ICON_ERROR
flag puts an error icon on the dialog.
MessageBoxEx.java
package com.zetcode; 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.MessageBox; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a simple MessageBox. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class MessageBoxEx { private Shell shell; public MessageBoxEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginTop = 50; layout.marginBottom = 150; layout.marginLeft = 50; layout.marginRight = 150; shell.setLayout(layout); Button msgBtn = new Button(shell, SWT.PUSH); msgBtn.setText("Show message"); msgBtn.addListener(SWT.Selection, event -> doShowMessageBox()); shell.setText("Message box"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void doShowMessageBox() { int style = SWT.ICON_INFORMATION | SWT.OK; MessageBox dia = new MessageBox(shell, style); dia.setText("Information"); dia.setMessage("Download completed."); dia.open(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); MessageBoxEx ex = new MessageBoxEx(display); display.dispose(); } }
There is one push button on the window. Clicking on the button displays an information message dialog.
int style = SWT.ICON_INFORMATION | SWT.OK;
The dialog contains an Information icon and an Ok button.
MessageBox dia = new MessageBox(shell, style);
An instance of the MessageBox
is created, taking the style as the second parameter.
dia.setText("Information");
The dialog's title is set with the setText()
method.
dia.setMessage("Download completed.");
The message is set with the setMessage()
method.

Application closing
In order to prevent data loss, data-centric applications often show a confirmation dialog when being closed. Only after the dialog is confirmed, the application terminates.
MessageBoxEx2.java
package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a confirmation dialog * when its closing is initiated. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class MessageBoxEx2 { private Shell shell; public MessageBoxEx2(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Close, event -> doShowMessageBox(event)); shell.setText("Message box"); shell.setSize(350, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void doShowMessageBox(Event event) { int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO; MessageBox messageBox = new MessageBox(shell, style); messageBox.setText("Information"); messageBox.setMessage("Really close application?"); event.doit = messageBox.open() == SWT.YES; } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); MessageBoxEx2 ex = new MessageBoxEx2(display); display.dispose(); } }
This example shows a confirmation dialog when its closing is initiated.
shell.addListener(SWT.Close, event -> doShowMessageBox(event));
We hook a listener to the SWT.Close
event type.
int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO;
The confirmation dialog is modal and contains a Question icon and Yes and No buttons. (Modal dialogs block the main application until they are dismissed.)
event.doit = messageBox.open() == SWT.YES;
Setting the doit
event to false cancels it; setting to true allows it.
Directory dialog
DirectoryDialog
is a dialog which is used to select a path to a certain directory.
DirectoryDialogEx.java
package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a directory dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class DirectoryDialogEx { private Shell shell; private Label status; public DirectoryDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); status = new Label(shell, SWT.BORDER); status.setText("Ready"); FormLayout layout = new FormLayout(); shell.setLayout(layout); FormData labelData = new FormData(); labelData.left = new FormAttachment(0); labelData.right = new FormAttachment(100); labelData.bottom = new FormAttachment(100); status.setLayoutData(labelData); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("DirectoryDialog"); shell.setSize(350, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { DirectoryDialog dialog = new DirectoryDialog(shell); String path = dialog.open(); if (path != null) { status.setText(path); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); DirectoryDialogEx ex = new DirectoryDialogEx(display); display.dispose(); } }
In our example, we select a directory with the DirectoryDialog
and display its path in the statusbar. The dialog is shown by clicking on the area of the window.
shell.addListener(SWT.MouseDown, event -> onMouseDown());
A mouse listener for the SWT.MouseDown
event is added to the shell. When we press a mouse button on the window, the onMouseDown()
method is invoked.
DirectoryDialog dialog = new DirectoryDialog(shell);
A DirectoryDialog
is created.
String path = dialog.open();
We get the path to the selected directory.
if (path != null) { status.setText(path); }
If the path is not null, we show the path in the status label.

FontDialog
FontDialog
is a dialog for selecting fonts. It is typically used in applications that do some text editing or formatting.
FontDialogEx.java
package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FontDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a font dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class FontDialogEx { private Shell shell; private Label label; public FontDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 100; layout.marginWidth = 100; shell.setLayout(layout); label = new Label(shell, SWT.NONE); label.setText("ZetCode Java SWT tutorial"); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("FontDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { FontDialog dialog = new FontDialog(shell); FontData fdata = dialog.open(); if (fdata != null) { Font font = new Font(shell.getDisplay(), fdata); label.setFont(font); label.pack(); shell.pack(); font.dispose(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); FontDialogEx ex = new FontDialogEx(display); display.dispose(); } }
In the code example, we use the FontDialog
to change the font of a label.
FontDialog dialog = new FontDialog(shell);
The FontDialog
is created.
Font font = new Font(shell.getDisplay(), fdata);
A Font
object is created from the font data, returned by the font dialog.
label.setFont(font);
The font is applied to the label with the setFont()
method.
label.pack(); shell.pack();
The pack()
method adapts the label and the shell to the new font type.
font.dispose();
Font is an OS resource; therefore, it must be disposed when not needed anymore.

ColorDialog
ColorDialog
is a dialog for selecting a colour.
ColorDialogEx.java
package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.ColorDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a color dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class ColorDialogEx { private Shell shell; private Label label; public ColorDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 100; layout.marginWidth = 100; shell.setLayout(layout); label = new Label(shell, SWT.NONE); label.setText("ZetCode Java SWT tutorial"); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("ColorDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { ColorDialog dialog = new ColorDialog(shell); RGB rgb = dialog.open(); if (rgb != null) { Color col = new Color(shell.getDisplay(), rgb); label.setForeground(col); col.dispose(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); ColorDialogEx ex = new ColorDialogEx(display); display.dispose(); } }
The example is very similar to the previous one. This time we change the colour of the label. The ColorDialog
is shown by clicking on the window area.
ColorDialog dialog = new ColorDialog(shell);
We create the ColorDialog
.
RGB rgb = dialog.open();
With the ColorDialog's
open()
method, we get the RGB value.
Color col = new Color(shell.getDisplay(), rgb); label.setForeground(col);
We get the colour value and modify the colour of the label.
col.dispose();
Color
is an OS resource, so we dispose it when it is not needed anymore.

FileDialog
FileDialog
is used to select a name of a file.
FileDialogEx.java
package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a file dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class FileDialogEx { private Shell shell; private Label label; public FileDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 50; layout.marginWidth = 50; shell.setLayout(layout); label = new Label(shell, SWT.NONE); String homeDir = System.getProperty("user.home"); label.setText(homeDir); label.pack(); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("FileDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { FileDialog dialog = new FileDialog(shell, SWT.OPEN); String[] filterNames = new String[] {"Java sources", "All Files (*)"}; String[] filterExtensions = new String[] {"*.java", "*"}; dialog.setFilterNames(filterNames); dialog.setFilterExtensions(filterExtensions); String path = dialog.open(); if (path != null) { label.setText(path); label.pack(); shell.pack(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); FileDialogEx ex = new FileDialogEx(display); display.dispose(); } }
The code example uses the FileDialog
to select a file. The dialog uses a filter to show only the Java sources. The name of the chosen file is shown in the label.
label = new Label(shell, SWT.NONE); String homeDir = System.getProperty("user.home"); label.setText(homeDir); label.pack();
At the start, the label widget displays the user's home directory.
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
We create a FileDialog
with the SWT.OPEN
flag. The dialog can be used both to open or save files. The save behaviour of the dialog is enabled with the SWT.SAVE
constant.
String[] filterNames = new String[] {"Java sources", "All Files (*)"}; String[] filterExtensions = new String[] {"*.java", "*"}; dialog.setFilterNames(filterNames); dialog.setFilterExtensions(filterExtensions);
We use two filters; one for Java sources and one for all file types.
String path = dialog.open();
The path name is retrieved with the FileDialog's
open()
method.
if (path != null) { label.setText(path); label.pack(); shell.pack(); }
The path name is set to the label with the setText()
method. The label and the shell are adapted to the size of the returned path with the pack()
method.
This part of the Java SWT tutorial was about dialogs in SWT.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论