You are on page 1of 25

Graphical User

Interface (GUI)
• Learn about Java basic GUI components
• Explore how the GUI components JFrame,
JLabel, JTextField and JButton
work
• Layout different components in a window
application
• Design and run an interactive window
application in Java
import javax.swing.JOptionPane;
public class RectangleGUI {
public static void main(String[] args) {
double width, length, area, perimeter;
String widthStr, lengthStr, outputStr;
lengthStr = JOptionPane.showInputDialog(null, "Enter Length: ");
length = Double.parseDouble(lengthStr);

widthStr = JOptionPane.showInputDialog(null, "Enter Width: ");


width = Double.parseDouble(widthStr);

area = length * width;


perimeter = (2 * length) + (2 * width);

outputStr = "Length: " + length + "\n" +


"Width: " + width + "\n" +
"Area: " + area + "sq. units \n" +
"Perimeter: " + perimeter + "units \n";

JOptionPane.showMessageDialog(null, outputStr);

System.exit(0);
}
}
JOptionPane Class
 JOptionPane is a standard, predefined class
that comes with every installation of Java used for
producing special windows—called dialog
windows, dialog boxes, or simply dialogs—that
either obtain input or display output from your
program. To make it available to your program,
you write

import javax.swing.JOptionPane;
showInputDialog() method
 The method showInputDialog produces a
dialog for obtaining input.

lengthStr = JOptionPane.showInputDialog
(null, "Enter Length: ");
length = Double.parseDouble(lengthStr);

widthStr = JOptionPane.showInputDialog
(null. "Enter Width: ");
width = Double.parseDouble(widthStr);
showInputDialog() method

Syntax:

String_Variable = JOptionPane.showInputDialog
(null, String_Expression);
showMessageDialog() method
• This method displays a dialog window that shows
some output.
outputStr = "Length: " + length + "\n" +
"Width: " + width + "\n" +
"Area: " + area + "sq. units \n" +
"Perimeter: " + perimeter + "units \n";

JOptionPane.showMessageDialog(null, outputStr);
showMessageDialog() method

Syntax:

JOptionPane.showMessageDialog(null,
String_Expression);
ICONS Used By JOptionPane

plain message
//default title and icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.");
//custom title, warning icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
warning", JOptionPane.WARNING_MESSAGE);
//custom title, error icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
error", JOptionPane.ERROR_MESSAGE);
//custom title, custom icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "Inane
custom dialog",
JOptionPane.INFORMATION_MESSAGE, icon);
//custom title, no icon
JOptionPane.showMessageDialog(null, "Eggs
aren’t supposed to be green.", "A plain
message", JOptionPane.PLAIN_MESSAGE);
System.exit(0) statement
 Required for an application program that uses a
windowing interface
 Really ends the program’s execution.

JOptionPane.showMessageDialog(null,
outputStr);

System.exit(0);
showConfirmDialog() method
 A confirmation dialog box gives buttons to select,
and when a user clicks one of the buttons, it
returns an integer value.
int selection;
do {
// play one Tic Tac Toe game
selection = JOptionPane.showConfirmDialog(null, //#1
"Would you like to play another Tic Tac Toe game?", //#2
"Confirmation", //#3
JOptionPane.YES_NO_OPTION); //#4
} while(selection == JOptionPane.YES_OPTION);
showConfirmDialog() method

int selection;
do {
// play one Tic Tac Toe game
selection = JOptionPane.showConfirmDialog(null, //#1
"Would you like to play another Tic Tac Toe game?", //#2
"Confirmation", //#3
JOptionPane.YES_NO_OPTION); //#4
} while(selection == JOptionPane.YES_OPTION);
.YES_NO_OPTION
.YES_NO_CANCEL_OPTION
.OK_CANCEL_OPTION
//default icon, custom title
int n =
JOptionPane.showConfirmDialog(
null, "Would you like green
eggs and ham?", "An Inane
Question",
JOptionPane.YES_NO_OPTION);

String[] options = {"Yes, please",


"No way!"};
int n =
JOptionPane.showOptionDialog(null,
"Would you like green eggs and ham?,
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
showOptionDialog() method
 An option dialog box allows a programmer to
create custom buttons using an array structure for
the button text options.
String[] options = {“Yes, please”,
“Hmm, I’m not sure”,
“No way!”};
int sel = JOptionPane.showOptionDialog(null, //#1
"Would you like green eggs and ham?”, //#2
"A Silly Question", //#3
JOptionPane.DEFAULT_OPTION, //#4
JOptionPane.QUESTION_MESSAGE, //#5
null, //#6
options, //#7
options[0]); //#8
String[] options = {“Yes, please”,
“Hmm, I’m not sure”,
“No way!”};
int sel = JOptionPane.showOptionDialog(null, #1
"Would you like green eggs and ham?”, #2
"A Silly Question", #3
JOptionPane.DEFAULT_OPTION, #4
JOptionPane.QUESTION_MESSAGE, #5
null, #6
options, #7
options[0]); #8

#1 – null to center the window


#2 – question/message displayed
#3 – title of the window
#4 – set of option buttons. DEFAULT_OPTION is used since buttons are defined
in the 7th argument
#5 – style of the message/icon displayed
#6 – additional icons
#7 – specifies the buttons (array that holds the button texts)
#8 – initial choice
YES_OPTION
NO_OPTION
CANCEL_OPTION
OK_OPTION
CLOSED_OPTION

Except for CLOSED_OPTION, each option corresponds to the


button the user pressed. When CLOSED_OPTION is
returned, it indicates that the user closed the dialog window
explicitly, rather than by choosing a button inside the option
pane.
import javax.swing.*;

public class DialogDefaultIcon {

public static void main(String[] args) {

final ImageIcon icon = new ImageIcon("smiley.png");


String[] str = {"Yes I Will",
"I'm Not Sure", "I Can't"};
int sel1 = JOptionPane.showOptionDialog(null,
"Will you go to the parade?",
"Confirm",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
icon,
str,
str[1]);
System.exit(0);
}
}
Streib, J. and Soma, T. (2014). Guide to Java: A Concise
Introduction to Programming. London: Springer-Verlag.

How to Make Dialogs.


https://docs.oracle.com/javase/tutorial/uiswing/compo
nents/dialog.html

You might also like