public class IODialog extends java.lang.Object implements IOModel
IODialog
class provides a simple, dialog-based mechanism
for interacting with the user. It is therefore similar to the
JOptionPane
facility in Swing (which it uses in the implementation).
The differences between the models are
IODialog
mechanism is considerably simpler, mostly
because it does not try to be as general.
IODialog
mechanism does not use static methods; clients
instantiate an IODialog
object and make calls on that object,
thereby emphasizing the object-oriented idea.
IODialog
class works even if Swing is not available.
IODialog
are intentionally the
same as those for the IOConsole
class so
that clients can substitute one model for another without making source changes.
The input methods are therefore
For example, the following code pops up the dialog shown on the right and
allows the user to enter an integer:
|
![]() |
Constructor and Description |
---|
IODialog()
Instantiates a new IODialog object that can then be used for dialog-based
input and output.
|
IODialog(java.awt.Component owner)
Instantiates a new IODialog object that can then be used for dialog-based
input and output.
|
Modifier and Type | Method and Description |
---|---|
boolean |
getAllowCancel()
Returns the state of the cancellation mode.
|
IOConsole |
getAssociatedConsole()
Returns the associated console for the dialog.
|
boolean |
getExceptionOnError()
Returns the state of the error-handling flag.
|
void |
print(boolean x)
Makes sure that
print can display a boolean . |
void |
print(char x)
Makes sure that
print can display a char . |
void |
print(double x)
Makes sure that
print can display a double . |
void |
print(float x)
Makes sure that
print can display a float . |
void |
print(int x)
Makes sure that
print can display an int . |
void |
print(long x)
Makes sure that
print can display a long . |
void |
print(java.lang.Object x)
Makes sure that
print can display an Object . |
void |
print(java.lang.String value)
Displays the argument value, allowing for the possibility of more
output in the same dialog.
|
void |
println()
Completes the output line and displays the dialog.
|
void |
println(boolean x)
Makes sure that
println can display a boolean . |
void |
println(char x)
Makes sure that
println can display a char . |
void |
println(double x)
Makes sure that
println can display a double . |
void |
println(float x)
Makes sure that
println can display a float . |
void |
println(int x)
Makes sure that
println can display an int . |
void |
println(long x)
Makes sure that
println can display a long . |
void |
println(java.lang.Object x)
Makes sure that
println can display an Object . |
void |
println(java.lang.String value)
Adds the value to the current output line and then displays the dialog.
|
boolean |
readBoolean()
Reads and returns a boolean value from the user, which must match
either
true or false , ignoring case. |
boolean |
readBoolean(java.lang.String prompt)
Prompts the user to enter a boolean value, which is then returned as
the value of this method.
|
boolean |
readBoolean(java.lang.String prompt,
java.lang.String trueLabel,
java.lang.String falseLabel)
Prompts the user to enter a value, which is interpreted as a boolean value
by matching it against the
trueLabel and falseLabel
parameters. |
double |
readDouble()
Reads and returns a double-precision value from the user.
|
double |
readDouble(double low,
double high)
Reads and returns a double-precision value from the user, which is
constrained to be within the specified inclusive range.
|
double |
readDouble(java.lang.String prompt)
Prompts the user to enter an double-precision number, which is then
returned as the value of this method.
|
double |
readDouble(java.lang.String prompt,
double low,
double high)
Prompts the user to enter an double-precision number, which is then returned
as the value of this method.
|
int |
readInt()
Reads and returns an integer value from the user.
|
int |
readInt(int low,
int high)
Reads and returns an integer value from the user, which is constrained to
be within the specified inclusive range.
|
int |
readInt(java.lang.String prompt)
Prompts the user to enter an integer, which is then returned as the value
of this method.
|
int |
readInt(java.lang.String prompt,
int low,
int high)
Prompts the user to enter an integer, which is then returned as the value
of this method.
|
java.lang.String |
readLine()
Reads and returns a line of input from the dialog, without
including the end-of-line characters that terminate the input.
|
java.lang.String |
readLine(java.lang.String prompt)
Prompts the user to enter a single character, which is then returned
as the value of this method.
|
void |
setAllowCancel(boolean flag)
Sets the cancellation mode of the dialog as specified by the
flag
parameter. |
void |
setAssociatedConsole(IOConsole console)
Establishes an associated console for the dialog.
|
void |
setExceptionOnError(boolean flag)
Sets the error-handling mode of the dialog as specified by the
flag
parameter. |
void |
showErrorMessage(java.lang.String msg)
Displays the error message in an error dialog.
|
public IODialog()
public IODialog(java.awt.Component owner)
owner
- A Component
used as the owner of created dialogspublic void print(java.lang.String value)
print
method is overloaded
so that value
can be of any type.public final void print(boolean x)
print
can display a boolean
.public final void print(char x)
print
can display a char
.public final void print(double x)
print
can display a double
.public final void print(float x)
print
can display a float
.public final void print(int x)
print
can display an int
.public final void print(long x)
print
can display a long
.public final void print(java.lang.Object x)
print
can display an Object
.public void println()
public void println(java.lang.String value)
println
method is overloaded so that value
can be of any type.public final void println(boolean x)
println
can display a boolean
.public final void println(char x)
println
can display a char
.public final void println(double x)
println
can display a double
.public final void println(float x)
println
can display a float
.public final void println(int x)
println
can display an int
.public final void println(long x)
println
can display a long
.public final void println(java.lang.Object x)
println
can display an Object
.public void showErrorMessage(java.lang.String msg)
showErrorMessage
in interface IOModel
msg
- The error msg to be displayedpublic final java.lang.String readLine()
public java.lang.String readLine(java.lang.String prompt)
public final int readInt()
setExceptionOnError
method.public final int readInt(int low, int high)
setExceptionOnError
method.public final int readInt(java.lang.String prompt)
setExceptionOnError
method.public int readInt(java.lang.String prompt, int low, int high)
low
and high
. If the user types a value that
is not a legal integer or is outside the specified range, the method
ordinarily offers the user a chance to reenter the data,
although this behavior can be changed using the
setExceptionOnError
method.public final double readDouble()
setExceptionOnError
method.readDouble
in interface IOModel
double
public final double readDouble(double low, double high)
setExceptionOnError
method.readDouble
in interface IOModel
low
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final double readDouble(java.lang.String prompt)
setExceptionOnError
method.readDouble
in interface IOModel
prompt
- The prompt string to display to the userdouble
public double readDouble(java.lang.String prompt, double low, double high)
low
and high
. If the user types a value
that is not a legal number, the method ordinarily offers the user a chance
to reenter the data, although this behavior can be changed using the
setExceptionOnError
method.readDouble
in interface IOModel
prompt
- The prompt string to display to the userlow
- The lowest value in the permitted rangehigh
- The highest value in the permitted rangedouble
public final boolean readBoolean()
true
or false
, ignoring case.
If the user types a value that is not one of these possibilities,
the method ordinarily offers the user a chance to reenter the data,
although this behavior can be changed using the
setExceptionOnError
method.readBoolean
in interface IOModel
public final boolean readBoolean(java.lang.String prompt)
setExceptionOnError
method.readBoolean
in interface IOModel
prompt
- The prompt string to display to the userpublic boolean readBoolean(java.lang.String prompt, java.lang.String trueLabel, java.lang.String falseLabel)
trueLabel
and falseLabel
parameters. If the user types a value that is not one of the two choices,
readBoolean
ordinarily offers the user a chance
to reenter the data, although this behavior can be changed using the
setExceptionOnError
method.readBoolean
in interface IOModel
prompt
- The prompt string to display to the usertrueLabel
- The label for the button indicating true
falseLabel
- The label for the button indicating false
public void setExceptionOnError(boolean flag)
flag
parameter. If flag
is false
(which is the default), the
input methods give the user a chance to retry after erroneous input. If this
value is set to true
, illegal input raises an
ErrorException
instead.flag
- false
to retry on errors; true
to raise an exceptionpublic boolean getExceptionOnError()
false
to retry
on errors; true
to raise an exception)public void setAllowCancel(boolean flag)
flag
parameter. If flag
is false
(which is the default),
the input methods do not include a "Cancel" button. If this value is set to
to true
, a "Cancel" button appears, which throws a
CancelledException
if it is invoked.flag
- false
to remove "Cancel" button; true
to enable itpublic boolean getAllowCancel()
false
to retry
on errors; true
to raise an exception)public void setAssociatedConsole(IOConsole console)
null
and the console is reading from an input stream,
then the dialog methods use the console for input instead of the dialog.console
- The associated consolepublic IOConsole getAssociatedConsole()