You are on page 1of 4

[IT ELECTIVE 1]

| Lesson 5

USING THE JOPTIONPANE CLASS FOR GUI INPUT


Two dialog boxes that can be used to accept user input are:
InputDialogPrompts the user for text input
ConfirmDialogAsks the user a question, providing buttons that the
user can click for Yes, No, and Cancel responses
Using Input Dialog Boxes
An input dialog box asks a question and provides a text field in which the
user can enter a response. You can create an input dialog box using the
showInputDialog() method. It returns a String that represents a users
response; this means that you can assign the showInputDialog() method to a
String variable and the variable will hold the value that the user enters.
Example:
import javax.swing.JOptionPane;
public class HelloNameDialog
{
public static void main(String[] args)
{
String result;
result = JOptionPane.showInputDialog(null, "What is
your name?");
JOptionPane.showMessageDialog(null, "Hello, " + result
+ "!");
}
}
Expected Output

Converting String to Numeric type


The term parse means to break into component parts. Grammarians talk
about parsing a sentence deconstructing it so as to describe its
grammatical components. Parsing a String converts it to its numeric
equivalent.
Example:
ACTS COMPUTER COLLEGE - INFANTA | by maria cristina b. nazareno

[IT ELECTIVE 1]

| Lesson 5

import javax.swing.JOptionPane;
public class SalaryDialog
{
public static void main(String[] args)
{
String wageString, dependentsString;
double wage, weeklyPay;
int dependents;
final double HOURS_IN_WEEK = 37.5;
wageString
=
JOptionPane.showInputDialog(null,
employee's hourly
wage",
"Salary
dialog
JOptionPane.INFORMATION_MESSAGE);
weeklyPay
HOURS_IN_WEEK;

"Enter
1",

Double.parseDouble(wageString)

dependentsString = JOptionPane.showInputDialog(null, "How


many
dependents?",
"Salary
dialog
2",
JOptionPane.QUESTION_MESSAGE);
dependents = Integer.parseInt(dependentsString);
JOptionPane.showMessageDialog(null, "Weekly salary is $" +
weeklyPay +
"\nDeductions will be made for " +
dependents + " dependents");
}
}
Using Confirm Dialog Boxes
A confirm dialog box displays the options Yes, No, and Cancel; you can
create one using the showConfirmDialog() method in the JOptionPane
class. The showConfirmDialog() method returns an integer containing one of
three
possible
values:
JOptionPane.YES_OPTION,
JOptionPane.NO_OPTION, or JOptionPane.CANCEL_OPTION.
Example:
import javax.swing.JOptionPane;
public class AirlineDialog
{
public static void main(String[] args)
ACTS COMPUTER COLLEGE - INFANTA | by maria cristina b. nazareno

[IT ELECTIVE 1]

| Lesson 5

{
int selection;
boolean isYes;
selection = JOptionPane.showConfirmDialog(null, "Do
you want to
upgrade to first class?");
isYes = (selection == JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "You responded "
+ isYes);
}
}

ACTS COMPUTER COLLEGE - INFANTA | by maria cristina b. nazareno

[IT ELECTIVE 1]

| Lesson 5

Expected Output

EXERCISES: Write an interactive class that accepts a users hourly rate of


pay and the number of hours worked. Display the users gross pay, the
withholding tax (15% of gross pay), and the net pay (gross pay
withholding). Save the class as Payroll.java.

ACTS COMPUTER COLLEGE - INFANTA | by maria cristina b. nazareno

You might also like