Skip to content

Instantly share code, notes, and snippets.

@zacscott
Last active December 17, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscott/5544306 to your computer and use it in GitHub Desktop.
Save zacscott/5544306 to your computer and use it in GitHub Desktop.
Provides some common swing dialogs in Java.
package net.zeddev.util;
import java.awt.Frame;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.util.logging.Logger;
/**
* <p>
* Provides some common swing dialogs.
* Supported dialogs include the following;
* <ul>
* <li>message - A simple message box.</li>
* <li>warning - A warning message box.</li>
* <li>error - A error message box.</li>
* <li>okcancel - A confirmation dialog.</li>
* <li>yesno - A another confirmation dialog.</li>
* <li>saveFile - A save file chooser dialog.</li>
* <li>openFile - An open file chooser dialog.</li>
* <li>selectDir - A directory chooser dialog</li>
* </ul>
* </p>
*
* <p>
* <b>NOTE:</b> This file is released as a stand-alone utility. The original
* file and the associated unit test can be found on GitHub Gist -
* <a href="https://gist.github.com/zscott92/5544306">here</a>.
* </p>
*
* @author Zachary Scott <zscott.dev@gmail.com>
*/
public final class SimpleDialog {
private static final Logger logger = Logger.getLogger(SimpleDialog.class.getName());
private SimpleDialog() {
}
/**
* Shows a info message box.
*
* @param parent The parent of the message box.
* @param title The title of the message box.
* @param msg The message to be displayed.
*/
public static void message(Frame parent, String title, String msg) {
JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.PLAIN_MESSAGE);
}
/**
* Shows a warning message box.
*
* @param parent The parent of the message box.
* @param msg The message to be displayed.
*/
public static void warning(Frame parent, String msg) {
JOptionPane.showMessageDialog(parent, msg, "WARNING", JOptionPane.WARNING_MESSAGE);
}
/**
* Shows a error message box.
*
* @param parent The parent of the message box.
* @param msg The message to be displayed.
*/
public static void error(Frame parent, String msg) {
JOptionPane.showMessageDialog(parent, msg, "ERROR", JOptionPane.ERROR_MESSAGE);
}
/**
* Displays a confirmation box with <code>Okay</code> and <code>Cancel</code>
* options.
*
* @param parent The parent of the message box.
* @param title The title of the message box.
* @param msg The question to be confirmed to be displayed.
* @return Whether the user pressed <code>Okay</code> or not.
*/
public static boolean okcancel(Frame parent, String title, String msg) {
return JOptionPane.showConfirmDialog(
parent, msg, title,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE
) == JOptionPane.OK_OPTION;
}
/**
* Displays a confirmation box with <code>Yes</code> and <code>No</code>
* options.
*
* @param parent The parent of the message box.
* @param title The title of the message box.
* @param msg The question to be confirmed to be displayed.
* @return Whether the user pressed <code>Yes</code> or not.
*/
public static boolean yesno(Frame parent, String title, String msg) {
return JOptionPane.showConfirmDialog(
parent, msg, title,
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE
) == JOptionPane.YES_OPTION;
}
/**
* Opens a save-file chooser dialog.
*
* @param parent The parent frame (can be <code>null</code>).
* @return The selected file (or <code>null</code> if error).
*/
public static File saveFile(final Frame parent) {
JFileChooser fileChooser = new JFileChooser();
int ret = fileChooser.showSaveDialog(parent);
if (ret == JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile();
} else if (ret == JFileChooser.ERROR_OPTION) {
logger.warning("Error occured with file chooser when saving.");
return null;
} else {
logger.info("User cancelled saving file.");
return null;
}
}
/**
* Opens a open-file chooser dialog.
*
* @param parent The parent frame (can be <code>null</code>).
* @return The selected file (or <code>null</code> if error).
*/
public static File openFile(final Frame parent) {
JFileChooser fileChooser = new JFileChooser();
int ret = fileChooser.showOpenDialog(parent);
if (ret == JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile();
} else if (ret == JFileChooser.ERROR_OPTION) {
logger.warning("Error occured with file chooser when opening file.");
return null;
} else {
logger.info("User cancelled choosing file.");
return null;
}
}
/**
* Opens a directory chooser dialog.
*
* @param parent The parent frame (can be <code>null</code>).
* @return The selected directory (or <code>null</code> if error).
*/
public static File selectDir(final Frame parent) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int ret = fileChooser.showDialog(parent, "Select");
if (ret == JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile();
} else if (ret == JFileChooser.ERROR_OPTION) {
logger.warning("Error occured with file chooser when selecting directory.");
return null;
} else {
logger.info("User cancelled selecting directory.");
return null;
}
}
}
/* Copyright (C) 2013 Zachary Scott <zscott.dev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment