There are situations where a dialog can be really nasty. For example, in a critical processing situation the application needs to ask the user for a decision, let's say about the deletion of some data. A normal modal dialog would do that sufficiently, but not when the decision is called from a loop, meaning the dialog appears over and over again, always asking the same question.
There may be a few situations where such repeats are actually necessary, but in general the user would like to answer the question just once for all elements in the loop. A popular example is when copying a backup-directory back to its origin, and the application asks for each there existing file whether it should overwrite it.
For that purpose the "Do-Not-Ask-Anymore Dialog" was invented.
The Swing JOptionPane.showConfirmDialog() dialog method does not have such a
functionality, but it offers means to implement such.
In object-oriented thinking, remembering a decision can be solved easily by a class that contains
(1) a field for the once "persisted" answer and
(2) a method that either shows a dialog or delivers the answer silently when it was already made "persistent".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import java.awt.*; import java.util.Objects; import javax.swing.*; /** * A modal confirm-dialog that may not show but deliver a persistent * answer when the user once activated its "Remember" checkbox. */ public class DoNotAskAnymoreConfirmDialog { private final String title; private final String message; private final String rememberAnswerText; private Boolean answer; public DoNotAskAnymoreConfirmDialog(String title, String message) { this(title, message, null); } public DoNotAskAnymoreConfirmDialog(String title, String message, String rememberAnswerText) { this.title = Objects.requireNonNull(title); this.message = Objects.requireNonNull(message); this.rememberAnswerText = (rememberAnswerText != null) ? rememberAnswerText : "Remember That and Don't Ask Anymore"; } public boolean answer(Component parent) { if (answer != null) // rememberAnswer checkbox has been selected once return answer.booleanValue(); final JLabel messageLabel = new JLabel(message); final JCheckBox rememberAnswer = new JCheckBox(rememberAnswerText, false); final JPanel panel = new JPanel(new BorderLayout()); panel.add(messageLabel, BorderLayout.CENTER); panel.add(rememberAnswer, BorderLayout.SOUTH); final boolean dialogAnswer = (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog( parent, panel, title, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) ); if (rememberAnswer.isSelected()) answer = Boolean.valueOf(dialogAnswer); return dialogAnswer; } } |
The constructors on lines 17 and 20 require that for each decision-semantic there must be a new constructor call.
That ensures that a dialog-object is not reused for a different decision and may silently
answer the wrong question.
The title and message parameters are required (not allowed to be null)
through Objects.requireNonNull(),
the rememberAnswerText is optional because the text on the checkbox will be always the same.
The instance-fields for the constructor parameters are final, so that the semantic never could be changed.
The answer() method on line 28 first looks at the answer field on line 15
and returns its value when not null.
It being null means the user did not yet activate the "Remember" checkbox.
In that case the dialog is shown on screen and the user can make a decision,
optionally activating the checkbox (line 47) to avoid repeated dialogs.
The answer field MUST NOT be static,
because then all dialog-semantics would use the same answer!
(This beautifully proves how valueable object-oriented thinking is.)
For sure it may be necessary to provide more answers than just TRUE and FALSE.
When you copy a backup-directory back to its origin,
you may want to ask whether to (1) overwrite or (2) merge all folders below that directory.
Then a Boolean answer may be not enough.
Feel free to abstract this class even more!








