@MinDoc(copyright="Copyright 1998 - 2009, 2014 A. Weinert", author="Albrecht Weinert", version="V.56", lastModified="28.06.2021", usage="construct and get answer (or static getAnswer)", purpose="dialog or info window with timeout") public class AskDialog extends Object implements ActionListener, KeyListener, DocumentListener
AskDialog
window.JOptionPane
used often in similar use
cases the main differences are:JOptionPane
) forbid the normal usage and ending
of a (non graphical) application.AskDialog
objects don't get exit() problems, even if non
graphical and also if not Frame4J or App
based.App
) safely.JDialog
respectively as
JFrame
(the common grandma is
Window
). As the event handling of Dialogs is problematic
on some platforms the usage of Frames should be preferred.YES
, NO
etc. differ
from those of JOptionPane
. Modifier and Type | Class and Description |
---|---|
static interface |
AskDialog.Listener
Listener for AskDialog.
|
Modifier and Type | Field and Description |
---|---|
protected int |
answer
The answer.
|
protected char[] |
answerText
The answer line.
|
Color |
bg
The dialogue's background colour.
|
static int |
CANCEL
Response value.
|
static int |
CLOSED
Response value.
|
protected AskDialog.Listener |
listener
The maximal one Listener.
|
int |
maxWait
Maximum wait time (in tenth of second) for human answer.
|
protected int |
minHeigth
Minimal height.
|
protected int |
minWidth
Minimal width.
|
boolean |
modal
Is it a modal Dialog or JDialog.
|
boolean |
multipleUse
Multiple usage of this object and of the dialogue.
|
static int |
NO
Response value.
|
static int |
NOT_YET
Response value.
|
boolean |
passWordHide
Password entry.
|
Window |
theDialog
The dialogue window.
|
static int |
YES
Response value.
|
Constructor and Description |
---|
AskDialog(Object parent,
String title,
boolean modal,
CharSequence upper,
CharSequence yes,
CharSequence cancel,
CharSequence no,
CharSequence lower)
Constructor with parent window / object.
|
AskDialog(String title,
CharSequence upper,
CharSequence yes,
CharSequence cancel,
CharSequence no,
CharSequence lower)
Parent-less Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent event)
Execution of button commands.
|
void |
addListener(AskDialog.Listener listener)
Register an answer Listener.
|
static String |
answerAsText(int answer)
Response (code) as text.
|
void |
changedUpdate(DocumentEvent e)
Changes in entry fields.
|
static void |
clearChars(char[] ca)
Clearing a char[].
|
void |
dispose()
Give up this AskDialog object and free all resources.
|
int |
getAnswer()
Wait for answer.
|
static int |
getAnswer(String title,
CharSequence upper,
boolean upperMonospaced,
CharSequence yes,
CharSequence cancel,
CharSequence no,
int waitMax,
CharSequence lower,
Color bg)
One time (parent-less) button inquiry.
|
int |
getAnswer(String upper,
String lower)
Wait for answer after setting new display texts.
|
String |
getAnswerText()
Wait for answer without setting new display texts.
|
static char[] |
getAnswerText(String title,
CharSequence upper,
boolean upperMonospaced,
CharSequence yes,
CharSequence cancel,
int waitMax,
CharSequence lower,
Color bg,
boolean passWordHide)
One time (parent-less) text inquiry.
|
char[] |
getAnswerText(String upper,
String lower,
boolean passWordHide)
Wait for answer after setting new display texts.
|
protected String |
getLab2String()
The (if given) lower text field's text.
|
protected char[] |
getLab2Text()
The (if given) lower text field's text.
|
int |
getMinHeigth()
Minimal height.
|
int |
getMinWidth()
Minimal width.
|
void |
insertUpdate(DocumentEvent e)
Changes in entry fields.
|
void |
keyPressed(KeyEvent event)
Execution of button commands.
|
void |
keyReleased(KeyEvent e)
Execution of button commands.
|
void |
keyTyped(KeyEvent e)
Execution of button commands.
|
void |
removeUpdate(DocumentEvent e)
Changes in entry fields.
|
void |
setMinHeigth(int minHeigth)
Minimal height.
|
void |
setMinWidth(int minWidth)
Minimal width.
|
void |
setUpperMonospaced()
Set the font for the upper text as non proportional.
|
void |
setVisible(String upper,
String lower)
Make visible without waiting for answer.
|
public static final int NOT_YET
-1
answer
,
maxWait
,
getAnswer()
,
Constant Field Valuespublic static final int CLOSED
-2
answer
,
Constant Field Valuespublic static final int CANCEL
0
answer
,
Constant Field Valuespublic static final int NO
1
answer
,
Constant Field Valuespublic static final int YES
2
answer
,
Constant Field Valuesprotected int minWidth
protected int minHeigth
public final Window theDialog
JDialog
or a JFrame
. This is decided finally at
construction.modal
public final boolean modal
protected volatile int answer
Value | Meaning | |
---|---|---|
-2 | CLOSED | window was explicitly closed (by X oder icon menu). |
-1 | NOT_YET | No response yet or timed out. |
0 | CANCEL | Cancel / middle button |
+1 | NO | No / right button |
+2 | YES | Yes / left button |
getAnswer()
,
getAnswer(String, String)
,
getAnswer()
protected volatile char[] answerText
answerText
will be set null.getAnswer()
public boolean multipleUse
public Color bg
public int maxWait
getAnswer()
. A value
>= 50 is used, the unit being 1/10 second. A value < 50 means
endless waiting in the thread having called
getAnswer()
.public boolean passWordHide
protected AskDialog.Listener listener
public AskDialog(Object parent, String title, boolean modal, CharSequence upper, CharSequence yes, CharSequence cancel, CharSequence no, CharSequence lower)
answer
or response may then be made by calling the
method getAnswer()
(as one possibility).JFrame
will be generated as dialogue window.Frame
,
Dialog
or App
. If parent is or has a window, a
modal dialogue is made and placed in the parent window's upper left
corner. Without a determinable parent window a JFrame
will be
made.parent
- the parent (window); of type Frame, Dialog, App, String
or nulltitle
- the title for this dialogue. if null and parent of type App
App.title
is used.modal
- true blocks the parent window (if given) until the closing
of this dialogueupper
- upper display line(s) (or null)yes
- the left (Yes) button's label (or null)cancel
- the middle (Cancel) button's label (or null)no
- the right (No) button's label (or null)lower
- lower display line (or null)public AskDialog(String title, CharSequence upper, CharSequence yes, CharSequence cancel, CharSequence no, CharSequence lower)
AskDialog
(null, title, false, upper,
yes, cancel, no, lower)
title
- the title for this dialogue.upper
- upper display line(s) (or null)yes
- the left (Yes) button's label (or null)cancel
- the middle (Cancel) button's label (or null)no
- the right (No) button's label (or null)lower
- lower display line (or null)public static int getAnswer(String title, CharSequence upper, boolean upperMonospaced, CharSequence yes, CharSequence cancel, CharSequence no, int waitMax, CharSequence lower, Color bg)
AskDialog
object for one time usage and the waiting for an answer in the calling
thread, guarded by settable time-out.title
- the title for this dialogue.App.title
is used.upper
- upper display line(s) (or null)yes
- the left (Yes) button's label (or null)cancel
- the middle (Cancel) button's label (or null)no
- the right (No) button's label (or null)lower
- lower display line (or null)upperMonospaced
- true means an upper text is displayed by a
non proportional fontbg
- background colour (or null meaning platform standard)getAnswer()
,
setUpperMonospaced()
,
AskDialog(Object, String, boolean, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence)
public static char[] getAnswerText(String title, CharSequence upper, boolean upperMonospaced, CharSequence yes, CharSequence cancel, int waitMax, CharSequence lower, Color bg, boolean passWordHide)
AskDialog
object for one time usage and the waiting for an answer in the calling
thread, guarded by adjustable time-out.title
- the title for this dialogue.App.title
is used.upper
- upper display line(s) (or null)yes
- the left (Yes) button's label (or null)cancel
- the middle (Cancel) button's label (or null)lower
- lower display line (or null)upperMonospaced
- true means an upper text is displayed by a
non proportional fontbg
- background Color (or null meaning platform standard)passWordHide
- see passWordHide
getAnswer()
,
setUpperMonospaced()
,
AskDialog(Object, String, boolean, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence)
,
clearChars(char[])
public static void clearChars(char[] ca)
getAnswerText(String, CharSequence, boolean, CharSequence, CharSequence, int, CharSequence, Color, boolean)
for password input for security.String
with passwords. String
s leave
by nature "non erasable" traces in memory.public static String answerAsText(int answer)
answer
public int getMinWidth()
public int getMinHeigth()
public void setMinWidth(int minWidth)
public void setMinHeigth(int minHeigth)
protected char[] getLab2Text()
protected String getLab2String()
public void setUpperMonospaced()
public void addListener(AskDialog.Listener listener)
AskDialog.Listener
object might be registered. To de-register this method is
to be called with null.askAnswered()
is called in an extra helper thread. So there are no restrictions
whatsoever on what an how much might be done in this method.public int getAnswer()
maxWait
is set accordingly, the waiting
for one of these events will be limited. If this time out happens this
method returns NOT_YET
as event code. Without time-out this
method will wait "forever" (blocking the caller's thread).answer
)getAnswer(String, String)
,
getAnswerText()
,
getAnswerText(String, String, boolean)
public int getAnswer(String upper, String lower)
getAnswer()
.upper
- If the AskDialog was made with an upper Text and this
parameter is not null, the upper text is set newly.lower
- the same for the lower textgetAnswerText()
,
getAnswer()
public char[] getAnswerText(String upper, String lower, boolean passWordHide)
getAnswer(upper, lower)
except for
returning the actual lower text, perhaps been edited by the user, if the
Window was closed by clicking the left Yes/OK button. If closed otherwise
or timed out null will be returned.upper
- If the AskDialog was made with an upper Text and this
parameter is not null, the upper text is set newly.lower
- the same for the lower textgetAnswer()
,
getAnswer(String, String)
,
getAnswerText()
public final String getAnswerText()
getAnswerText(null, null, false)
with transforming the returned value
to a String.getAnswer()
,
getAnswer(String, String)
public void setVisible(String upper, String lower)
answer
may be checked cyclicly, getAnswer()
may
be called (later) or a AskDialog.Listener
added by
addListener()
may be informed.upper
- If the AskDialog was made with an upper Text and this
parameter is not null, the upper text is set newly.lower
- the same for the lower textpublic void dispose()
multipleUse
is
set true prior to first use. In this case (only) this object holds
graphical resources and threads. So its recommended (and sometimes
necessary) to free those after last use.public void actionPerformed(ActionEvent event)
actionPerformed
in interface ActionListener
public void keyTyped(KeyEvent e)
keyTyped
in interface KeyListener
public void keyReleased(KeyEvent e)
keyReleased
in interface KeyListener
public void keyPressed(KeyEvent event)
keyPressed
in interface KeyListener
public final void changedUpdate(DocumentEvent e)
changedUpdate
in interface DocumentListener
public final void insertUpdate(DocumentEvent e)
insertUpdate
in interface DocumentListener
public final void removeUpdate(DocumentEvent e)
removeUpdate
in interface DocumentListener