Example usage for javax.swing JOptionPane showOptionDialog

List of usage examples for javax.swing JOptionPane showOptionDialog

Introduction

In this page you can find the example usage for javax.swing JOptionPane showOptionDialog.

Prototype

@SuppressWarnings("deprecation")
public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
        int messageType, Icon icon, Object[] options, Object initialValue) throws HeadlessException 

Source Link

Document

Brings up a dialog with a specified icon, where the initial choice is determined by the initialValue parameter and the number of choices is determined by the optionType parameter.

Usage

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * Quit Tangara (close the window and the program).
 *
 *///from   ww  w. ja  v  a  2s  .c  om
public void exit() {
    graphicsPane.freeze(true);
    String title = Messages.getString("EditorFrame.exit.title"); //$NON-NLS-1$
    String message = Messages.getString("EditorFrame.exit.message"); //$NON-NLS-1$
    Object[] options = { Messages.getString("tangara.yes"), //$NON-NLS-1$
            Messages.getString("tangara.cancel") }; //$NON-NLS-1$
    int answer = JOptionPane.showOptionDialog(this, message, title, optionType, JOptionPane.QUESTION_MESSAGE,
            null, // do not use a custom Icon
            options, // the titles of buttons
            options[0]);

    if (answer == JOptionPane.OK_OPTION) {
        if (getProgramManager().checkProgramChanged()) {
            dispose();
            Program.instance().exit();
        }
    } else
        graphicsPane.freeze(false);
}

From source file:org.colombbus.tangara.EditorFrame.java

/**
 * This method initializes refreshButton
 *
 * @return javax.swing.JButton/*from  w  ww.  j ava  2 s .  c  o  m*/
 */
private JButton getRefreshButton() {
    if (refreshButton == null) {
        refreshButton = new JButton();
        refreshButton.setBackground(new Color(156, 199, 213));
        refreshButton.setText(Messages.getString("EditorFrame.button.clean")); //$NON-NLS-1$
        refreshButton.setFont(new Font("Lucida Grande", Font.PLAIN, 13)); //$NON-NLS-1$
        refreshButton.setIcon(new ImageIcon(getClass().getResource("/org/colombbus/tangara/cross.png"))); //$NON-NLS-1$
        refreshButton.setPreferredSize(new Dimension(75, 30));
        refreshButton.setForeground(new Color(51, 51, 51));
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                graphicsPane.freeze(true);
                String message = Messages.getString("EditorFrame.confirmClean.text"); //$NON-NLS-1$
                String title = Messages.getString("EditorFrame.confirmClean.title"); //$NON-NLS-1$
                Object[] options = { Messages.getString("tangara.yes"), Messages.getString("tangara.cancel") }; //$NON-NLS-1$ //$NON-NLS-2$
                int answer = JOptionPane.showOptionDialog(EditorFrame.this, message, title, optionType,
                        JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                if (answer == JOptionPane.OK_OPTION) {
                    refresh();
                }
                graphicsPane.freeze(false);
                commandPane.requestFocus();
            }
        });
    }
    return refreshButton;
}

From source file:org.colombbus.tangara.net.FileReceptionDialog.java

/**
 * This method initializes this//ww  w .ja  v a2 s . com
 * 
 */
public void setVisible() {
    final String[] options = { Messages.getString("FileReceptionDialog.saveButton"),
            Messages.getString("FileReceptionDialog.cancelButton") };
    final String title = Messages.getString("FileReceptionDialog.title");
    final int optionType = JOptionPane.OK_CANCEL_OPTION;
    final int messageType = JOptionPane.QUESTION_MESSAGE;
    final Icon icon = null;

    int choosenOption = JOptionPane.showOptionDialog(owner, message, title, optionType, messageType, icon,
            options, options[0]);
    if (choosenOption == JOptionPane.OK_OPTION)
        setTargetFile();
}

From source file:org.colombbus.tangara.net.FileReceptionDialog.java

private void setTargetFile() {
    File targetDir = Configuration.instance().getUserHome();
    JFileChooser chooserDlg = new JFileChooser(targetDir);

    String filterMsg = Messages.getString("FileReceptionDialog.filter.allFiles");
    SimpleFileFilter filter = new SimpleFileFilter(filterMsg);
    chooserDlg.setFileFilter(filter);/*from   www.j a  va  2  s  .  c o  m*/

    File originalSelFile = new File(targetDir, sourceFilename);
    File curSelFile = originalSelFile;
    boolean showDialog = true;
    while (showDialog) {
        chooserDlg.setSelectedFile(curSelFile);
        int userAction = chooserDlg.showSaveDialog(owner);
        curSelFile = chooserDlg.getSelectedFile();
        switch (userAction) {
        case JFileChooser.CANCEL_OPTION:
            showDialog = false;
            break;
        case JFileChooser.APPROVE_OPTION:
            if (curSelFile.exists()) {
                String title = Messages.getString("FileReceptionDialog.overwrite.title");
                String msgPattern = Messages.getString("FileReceptionDialog.overwrite.message");
                String overwriteMsg = MessageFormat.format(msgPattern, curSelFile.getName());
                Object[] options = { Messages.getString("FileReceptionDialog.yes"),
                        Messages.getString("FileReceptionDialog.no") };
                int userChoice = JOptionPane.showOptionDialog(owner, overwriteMsg, title,
                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a
                        // custom Icon
                        options, // the titles of buttons
                        options[0]);
                if (userChoice == JOptionPane.OK_OPTION) {
                    if (saveFileAs(curSelFile)) {
                        showDialog = false;
                    }
                }
            } else if (saveFileAs(curSelFile)) {
                showDialog = false;
            }
            break;
        case JFileChooser.ERROR_OPTION:
            LOG.error("Error in file chooser dialog");
            // TODO what to do in case of error ? Retry ?
            showDialog = false;
            break;
        }
    }
}

From source file:org.datacleaner.windows.AnalysisJobBuilderWindowImpl.java

@Override
protected boolean onWindowClosing() {
    if (!super.onWindowClosing()) {
        return false;
    }/*from  w  ww .  java 2  s. c o  m*/

    switch (_currentPanelType) {
    case WELCOME:
        final int count = getWindowContext().getWindowCount(AnalysisJobBuilderWindow.class);
        if (count == 1) {
            if (getWindowContext().showExitDialog()) {
                cleanupForWindowClose();
                getWindowContext().exit();
            }
        } else {
            cleanupForWindowClose();
            return true;
        }
        break;
    case EDITING_CONTEXT:
        // if datastore is set and datastore selection is enabled,
        // return to datastore selection.

        if (isJobUnsaved(getJobFile(), _analysisJobBuilder) && (_saveButton.isEnabled())) {

            final Object[] buttons = { "Save changes", "Discard changes", "Cancel" };
            final int unsavedChangesChoice = JOptionPane.showOptionDialog(this,
                    "The job has unsaved changes. What would you like to do?", "Unsaved changes detected",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, buttons, buttons[1]);

            if (unsavedChangesChoice == 0) { // save changes
                _saveButton.doClick();
            } else if (unsavedChangesChoice != 1) { // cancel closing
                return false;
            }
        }

        resetJob();
        break;
    default:
        changePanel(AnalysisWindowPanelType.WELCOME);
    }

    return false;
}

From source file:org.datavyu.views.DataControllerV.java

/**
 * Presents a confirmation dialog when removing a plugin from the project.
 * @return True if the plugin should be removed, false otherwise.
 *//*from  ww w .j  a v a 2s.  c o m*/
private boolean shouldRemove() {
    ResourceMap rMap = Application.getInstance(Datavyu.class).getContext().getResourceMap(Datavyu.class);

    String cancel = "Cancel";
    String ok = "OK";

    String[] options = new String[2];

    if (Datavyu.getPlatform() == Platform.MAC) {
        options[0] = cancel;
        options[1] = ok;
    } else {
        options[0] = ok;
        options[1] = cancel;
    }

    int selection = JOptionPane.showOptionDialog(this, rMap.getString("ClosePluginDialog.message"),
            rMap.getString("ClosePluginDialog.title"), JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, options, cancel);

    // Button behaviour is platform dependent.
    return (Datavyu.getPlatform() == Platform.MAC) ? (selection == 1) : (selection == 0);
}

From source file:org.docx4all.ui.main.WordMLEditor.java

public int showConfirmDialog(String title, String message, int optionType, int messageType, Object[] options,
        Object initialValue) {// w ww .j a  va2s .  c om

    return JOptionPane.showOptionDialog(getWindowFrame(), message, title, optionType, messageType, null,
            options, initialValue);
}

From source file:org.fhaes.gui.AnalysisResultsPanel.java

/**
 * Save a file to disk, asking the user for a filename. Add the selected FileFilter to the save dialog box.
 * /*from  w w w . j a v a  2s  . c  o m*/
 * @param fileToSave
 * @param filter
 */
private void saveFileToDisk(File fileToSave, FileFilter filter) {

    File outfile = IOUtil.getOutputFile(filter);

    if (outfile == null)
        return;

    if (outfile.exists()) {
        Object[] options = { "Overwrite", "No", "Cancel" };
        int response = JOptionPane.showOptionDialog(App.mainFrame,
                "This file already exists.  Are you sure you want to overwrite?", "Confirm",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a custom Icon
                options, // the titles of buttons
                options[0]); // default button title

        if (response != JOptionPane.YES_OPTION) {
            return;
        }

    }

    try {
        FileUtils.copyFile(fileToSave, outfile);
    } catch (IOException e) {
        JOptionPane.showMessageDialog(App.mainFrame, "Error saving file:\n" + e.getMessage());
        e.printStackTrace();
    }
}

From source file:org.fhaes.gui.ShapeFileDialog.java

/**
 * Prompt the user for an output filename
 * /*from   w  w  w  . j a  va2s .c o  m*/
 * @param filter
 * @return
 */
private File getOutputFile(FileFilter filter) {

    String lastVisitedFolder = App.prefs.getPref(PrefKey.PREF_LAST_EXPORT_FOLDER, null);
    File outputFile;

    // Create a file chooser
    final JFileChooser fc = new JFileChooser(lastVisitedFolder);

    fc.setAcceptAllFileFilterUsed(true);

    if (filter != null) {
        fc.addChoosableFileFilter(filter);
        fc.setFileFilter(filter);
    }

    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fc.setMultiSelectionEnabled(false);
    fc.setDialogTitle("Save as...");

    // In response to a button click:
    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        outputFile = fc.getSelectedFile();

        if (FileUtils.getExtension(outputFile.getAbsolutePath()) == "") {
            log.debug("Output file extension not set by user");

            if (fc.getFileFilter().getDescription().equals(new SHPFileFilter().getDescription())) {
                log.debug("Adding shp extension to output file name");
                outputFile = new File(outputFile.getAbsolutePath() + ".shp");
            }
        } else {
            log.debug("Output file extension set my user to '"
                    + FileUtils.getExtension(outputFile.getAbsolutePath()) + "'");
        }

        App.prefs.setPref(PrefKey.PREF_LAST_EXPORT_FOLDER, outputFile.getAbsolutePath());
    } else {
        return null;
    }

    if (outputFile.exists()) {
        Object[] options = { "Overwrite", "No", "Cancel" };
        int response = JOptionPane.showOptionDialog(this,
                "The file '" + outputFile.getName() + "' already exists.  Are you sure you want to overwrite?",
                "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, // do not use a custom Icon
                options, // the titles of buttons
                options[0]); // default button title

        if (response != JOptionPane.YES_OPTION) {
            return null;
        }
    }

    return outputFile;
}

From source file:org.gcaldaemon.gui.ConfigEditor.java

public final void error(String title, String message, Exception e) {
    if (message == null || message.length() < 5) {
        message = e.getClass().getName() + ": " + message; //$NON-NLS-1$
    }/* ww  w . j  a  v a  2  s .  c  o  m*/
    log.error(message, e);
    status(message);
    TOOLKIT.beep();
    String[] options = new String[1];
    options[0] = Messages.getString("ok"); //$NON-NLS-1$
    JOptionPane.showOptionDialog(this, message, title, JOptionPane.OK_OPTION, JOptionPane.ERROR_MESSAGE, null,
            options, null);
}