0% found this document useful (0 votes)
274 views4 pages

Two Dialog Boxes That Can Be Used To Accept User Input Are

The document discusses using the JOptionPane class in Java for graphical user interface (GUI) input. It describes two types of dialog boxes - input dialogs and confirm dialogs. Input dialogs prompt for text input using the showInputDialog() method, while confirm dialogs ask yes/no questions and display buttons for Yes, No, and Cancel using the showConfirmDialog() method. Examples are provided to get user input via these dialog boxes and convert the input to numeric types as needed. The document concludes with an exercise to create an interactive payroll calculator class using these dialog box methods.
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)
274 views4 pages

Two Dialog Boxes That Can Be Used To Accept User Input Are

The document discusses using the JOptionPane class in Java for graphical user interface (GUI) input. It describes two types of dialog boxes - input dialogs and confirm dialogs. Input dialogs prompt for text input using the showInputDialog() method, while confirm dialogs ask yes/no questions and display buttons for Yes, No, and Cancel using the showConfirmDialog() method. Examples are provided to get user input via these dialog boxes and convert the input to numeric types as needed. The document concludes with an exercise to create an interactive payroll calculator class using these dialog box methods.
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/ 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