Dialogs
In this part of the Java SWT tutorial, we will 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.
Directory dialog
The directory dialog is a dialog, which is used to select a path to a certain directory.package com.zetcode;In our example, we use the directory dialog to select a directory. The path to the directory is shown in the status label. The dialog is shown by clicking on the area of the window.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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 2009
*/
public class SWTApp {
private Shell shell;
public SWTApp(Display display) {
shell = new Shell(display);
shell.setText("DirectoryDialog");
initUI();
shell.setSize(350, 250);
shell.setLocation(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
final Label 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.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent event) {
DirectoryDialog dialog = new DirectoryDialog(shell);
String path = dialog.open();
if (path != null)
status.setText(path);
}
});
}
public static void main(String[] args) {
Display display = new Display();
new SWTApp(display);
display.dispose();
}
}
DirectoryDialog dialog = new DirectoryDialog(shell);
DirectoryDialog
is created. String path = dialog.open();We get the path to the selected directory.
if (path != null)If the path is not null, we show the path in the status label.
status.setText(path);
Figure: Directory dialog
FontDialog
TheFontDialog
is a dialog for selecting fonts. It is typically used in applications, that do some text editing or formatting. package com.zetcode;In the code example, we use a
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
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 2009
*/
public class SWTApp {
private Shell shell;
public SWTApp(Display display) {
shell = new Shell(display);
shell.setText("FontDialog");
initUI();
shell.setSize(350, 250);
shell.setLocation(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
final Label label = new Label(shell, SWT.NONE);
label.setText("ZetCode Java SWT tutorial");
label.setLocation(50, 50);
label.pack();
shell.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent event) {
FontDialog dialog = new FontDialog(shell);
FontData fdata = dialog.open();
if (fdata != null) {
Font font = new Font(shell.getDisplay(), fdata);
label.setFont(font);
label.pack();
font.dispose();
}
}
});
}
public static void main(String[] args) {
Display display = new Display();
new SWTApp(display);
display.dispose();
}
}
FontDialog
to change the font of a label. FontDialog dialog = new FontDialog(shell);We create the
FontDialog
. Font font = new Font(shell.getDisplay(), fdata);
Font
object is created from the font data, returned by the font dialog. Figure: FontDialog
ColorDialog
ColorDialog
is a dialog for selecting a color. package com.zetcode;The example is very similar to the previous one. This time we change the color of the label.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
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 2009
*/
public class SWTApp {
private Shell shell;
public SWTApp(Display display) {
shell = new Shell(display);
shell.setText("ColorDialog");
initUI();
shell.setSize(350, 250);
shell.setLocation(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
final Label label = new Label(shell, SWT.NONE);
label.setText("ZetCode Java SWT tutorial");
label.setLocation(50, 50);
label.pack();
shell.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent event) {
ColorDialog dialog = new ColorDialog(shell);
RGB rgb = dialog.open();
if (rgb != null) {
Color col = new Color(shell.getDisplay(), rgb);
label.setForeground(col);
col.dispose();
}
}
});
}
public static void main(String[] args) {
Display display = new Display();
new SWTApp(display);
display.dispose();
}
}
ColorDialog dialog = new ColorDialog(shell);We create the
ColorDialog
. RGB rgb = dialog.open();We get the RGB value.
Color col = new Color(shell.getDisplay(), rgb);We get the color value and modify the color of the label.
label.setForeground(col);
Figure: ColorDialog
FileDialog
TheFileDialog
is used to select a name of a file. package com.zetcode;The code example uses a
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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 2009
*/
public class SWTApp {
private Shell shell;
public SWTApp(Display display) {
shell = new Shell(display);
shell.setText("FileDialog");
initUI();
shell.setSize(350, 250);
shell.setLocation(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
final Label label = new Label(shell, SWT.NONE);
label.setText("ZetCode Java SWT tutorial");
label.setLocation(50, 50);
label.pack();
shell.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent event) {
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();
}
}
});
}
public static void main(String[] args) {
Display display = new Display();
new SWTApp(display);
display.dispose();
}
}
FileDialog
to select a file. The dialog uses a filter to show only the java sources. The name of the file is shown in the label. FileDialog dialog = new FileDialog(shell, SWT.OPEN);We create an
FileDialog
with a SWT.OPEN
flag. The dialog can be used to open or save files. String[] filterNames = new String[]We use a filter to narrow the files to Java sources.
{"Java sources", "All Files (*)"};
String[] filterExtensions = new String[]
{"*.java", "*"};
dialog.setFilterNames(filterNames);
dialog.setFilterExtensions(filterExtensions);
This part of the Java SWT tutorial was about dialogs in SWT.
No comments:
Post a Comment