[go: up one dir, main page]

0% found this document useful (0 votes)
18 views7 pages

JOption

Java JOption

Uploaded by

Tamiko Chiraki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

JOption

Java JOption

Uploaded by

Tamiko Chiraki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JOption Pane Dialogs

A dialog box is typically used as a temporary window to receive additional information from the
user. It is also used to provide a notification or confirmation that certain events have occurred.
The JOptionPane class can be used to create four standard dialog boxes, namely: message
dialog box, confirmation dialog box, input dialog box, and option dialog box.

The four standard dialogs of JOptionPane class.

Standard Dialog Description

Message Dialog Displays an error, information, warning message


with an OK button.
Confirmation Dialog Displays a question and asks for the user’s
confirmation from the list of default options
(OK/YES/NO/CANCEL).
Input Dialog Displays a question and gets the user’s input
from the text field, combo box or list

Option Dialog Displays a question and get the user’s response


from the set of customized options.

Message Dialog
A message dialog displays a message that alerts the user and waits for the OK button to be
clicked then dismisses it. This type of dialog does not return any value. The method for creating
a message dialog is shown below:
The syntax for the message dialog.
JOptionPane.showMessageDialog(

Component parentComponent,

Object message,

String title,

int messageType,
1. parentComponent – This parameter can be any component or null that determines
which JFrame would be the parent of the dialog box.
2. message – The second parameter is used to place any objects. But often, a String is
used to display a message to the dialog.
NOTE: The message dialog will work as long as the two given parameters are
supplied (i.e Parent component and Object).
3. title – The title is a string displayed in the title bar of the dialog with the default
value "Message".
4. messageType – This parameter can be a number or a constant value that
determines the type of message dialog to be used. See the table below for the list of
message dialog types:
Different dialog types of Java.

Int
Message Dialog Type Icon Description
value
A message that
PLAIN_MESSAGE -1 Blank contains a message but
no icon.
A dialog that indicates
ERROR_MESSAGE 0
a program error.
This dialog indicates an
INFORMATION_MESSAGE 1
informational message.
This message dialog
WARNING_MESSAGE 2 warns for potential
problem in a program.
This dialog displays a
question. This dialog
QUESTION_MESSAGE 3
usually requires a
button for a response.

5. icon – The icon to display in a message dialog. An Icon in the dialog boxes can be
specified using the ImageIcon class. ImageIcon class supports JPEG, PNG and GIF
files.
Example program for an information message dialog.

Output of Code

Input Dialog
An input dialog box is used to get input from the user. In this type of dialog, input can be
entered from a text field, combo box or a list. You can specify the possible values in the input
dialog using an array, and one of these can be assigned as the initial value. The method for
creating an input dialog is shown below:

JOptionPane.showInputDialog(Component
parentComponent,

Object message,

String title,

int messageType,

Icon icon

Object[] selectionValues,
1. parentComponent – This parameter can be any component or null that determines
which JFrame would be the parent of the dialog box.
2. message – The second parameter is used to place any objects. But often, a String is
used to display a message to the dialog.
3. title – This parameter is a String used as a title displayed in the title bar of the
message dialog.
4. messageType – This parameter can be a number or a constant value that
determines the type of message dialog to be used.
5. icon - The icon to display in an input dialog.
6. selectionValues – This parameter is an array of Objects that store the possible
options of the input dialog.
7. initialSelectedValue – This parameter sets which index in the array would be the
initial value of the input dialog.
When creating an input dialog, if the selectionValues are not specified, the input dialog will
display a text field. When you create an input dialog with selectionValues that has fewer than
20 (twenty) options, the input dialog will display a combo box. When you create an input dialog
with selectionValues that are more than 20, the input dialog will display a list.
Sample program that will display a text field in the input dialog.

Output of Code

Sample program that will display a combo box in the input dialog.
The output of the code above:

A program that will display a list in the input dialog

Output of Code
Option Dialog
An option dialog allows you to create a customize buttons. You can create an option dialog
using the following method:
Syntax for the option dialog.

JOptionPane.showOptionDialog(Component parentComponent,
Object message,
String title,
int messageType,
Icon icon
Object[] selectionValues,
Object initialSelectedValue

1. parentComponent – This parameter can be any component or null that determines


which JFrame would be the parent of the dialog box.
2. message – The second parameter is used to place any objects. But often, a String is
used to display a message to the dialog.
3. title – This parameter is a String used as a title displayed in the title bar of the option
dialog.
4. messageType – This parameter can be a number or a constant value that
determines the type of message dialog to be used.
5. icon - The icon to display in an option dialog.
6. selectionValues – This parameter is an array of Objects that store the possible
options of the option dialog.
7. initialSelectedValue – This parameter sets which index in the array would be the
initial value of the option dialog.

Sample program for an option dialog with customized option names.


Output of Code

https://www.youtube.com/watch?v=GLUA5maVxoY

https://www.youtube.com/watch?v=E3JQrxxQKIo

https://www.youtube.com/watch?v=KDUwt27MC5U

https://www.youtube.com/watch?v=Tw82LiP__hU

You might also like