Java Code Examples for javax.swing.JOptionPane#addPropertyChangeListener()

The following examples show how to use javax.swing.JOptionPane#addPropertyChangeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomDialog.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
/** Creates the reusable dialog. */
public CustomDialog(Frame aFrame, String aWord, DialogDemo parent) {
    super(aFrame, true);
    dd = parent;

    magicWord = aWord.toUpperCase();
    setTitle("Quiz");

    textField = new JTextField(10);

    // Create an array of the text and components to be displayed.
    String msgString1 = "What was Dr. SEUSS's real last name?";
    String msgString2 = "(The answer is \"" + magicWord + "\".)";
    Object[] array = { msgString1, msgString2, textField };

    // Create an array specifying the number of dialog buttons
    // and their text.
    Object[] options = { btnString1, btnString2 };

    // Create the JOptionPane.
    optionPane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);

    // Make this dialog display it.
    setContentPane(optionPane);

    // Handle window closing correctly.
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent we) {
            /*
             * Instead of directly closing the window, we're going to change
             * the JOptionPane's value property.
             */
            optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
        }
    });

    // Ensure the text field always gets the first focus.
    addComponentListener(new ComponentAdapter() {
        public void componentShown(ComponentEvent ce) {
            textField.requestFocusInWindow();
        }
    });

    // Register an event handler that puts the text into the option pane.
    textField.addActionListener(this);

    // Register an event handler that reacts to option pane state changes.
    optionPane.addPropertyChangeListener(this);
}
 
Example 2
Source File: BookActions.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Prompts the user for interactive confirmation or modification of
 * book/page parameters
 *
 * @param stub the current sheet stub, or null
 * @return true if parameters are applied, false otherwise
 */
private static boolean applyUserSettings (final SheetStub stub)
{
    try {
        final WrappedBoolean apply = new WrappedBoolean(false);
        final ScoreParameters scoreParams = new ScoreParameters(stub);
        final JOptionPane optionPane = new JOptionPane(
                scoreParams.getComponent(),
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION);
        final String frameTitle = (stub != null) ? (stub.getBook().getRadix() + " parameters")
                : "General parameters";
        final JDialog dialog = new JDialog(OMR.gui.getFrame(), frameTitle, true); // Modal flag
        dialog.setContentPane(optionPane);
        dialog.setName("scoreParams");

        optionPane.addPropertyChangeListener(new PropertyChangeListener()
        {
            @Override
            public void propertyChange (PropertyChangeEvent e)
            {
                String prop = e.getPropertyName();

                if (dialog.isVisible() && (e.getSource() == optionPane)
                            && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                    Object obj = optionPane.getValue();
                    int value = (Integer) obj;
                    apply.set(value == JOptionPane.OK_OPTION);

                    // Exit only if user gives up or enters correct data
                    if (!apply.isSet() || scoreParams.commit(stub)) {
                        dialog.setVisible(false);
                        dialog.dispose();
                    } else {
                        // Incorrect data, so don't exit yet
                        try {
                            // TODO: Is there a more civilized way?
                            optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                        } catch (Exception ignored) {
                        }
                    }
                }
            }
        });

        dialog.pack();
        OmrGui.getApplication().show(dialog);

        return apply.value;
    } catch (Exception ex) {
        logger.warn("Error in ScoreParameters", ex);

        return false;
    }
}
 
Example 3
Source File: ScoreActions.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Prompts the user for interactive confirmation or modification of
 * score/page parameters
 *
 * @param sheet the current sheet, or null
 * @return true if parameters are applied, false otherwise
 */
private static boolean applyUserSettings (final Sheet sheet)
{
    try {
        final WrappedBoolean apply = new WrappedBoolean(false);
        final ScoreParameters scoreParams = new ScoreParameters(sheet);
        final JOptionPane optionPane = new JOptionPane(
                scoreParams.getComponent(),
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION);
        final String frameTitle = (sheet != null)
                ? (sheet.getScore()
                .getRadix()
                   + " parameters")
                : "General parameters";
        final JDialog dialog = new JDialog(
                Main.getGui().getFrame(),
                frameTitle,
                true); // Modal flag
        dialog.setContentPane(optionPane);
        dialog.setName("scoreParams");

        optionPane.addPropertyChangeListener(
                new PropertyChangeListener()
        {
            @Override
            public void propertyChange (PropertyChangeEvent e)
            {
                String prop = e.getPropertyName();

                if (dialog.isVisible()
                    && (e.getSource() == optionPane)
                    && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                    Object obj = optionPane.getValue();
                    int value = ((Integer) obj).intValue();
                    apply.set(value == JOptionPane.OK_OPTION);

                    // Exit only if user gives up or enters correct data
                    if (!apply.isSet()
                        || scoreParams.commit(sheet)) {
                        dialog.setVisible(false);
                        dialog.dispose();
                    } else {
                        // Incorrect data, so don't exit yet
                        try {
                            // TODO: Is there a more civilized way?
                            optionPane.setValue(
                                    JOptionPane.UNINITIALIZED_VALUE);
                        } catch (Exception ignored) {
                        }
                    }
                }
            }
        });

        dialog.pack();
        MainGui.getInstance()
                .show(dialog);

        return apply.value;
    } catch (Exception ex) {
        logger.warn("Error in ScoreParameters", ex);

        return false;
    }
}
 
Example 4
Source File: LicenseCompanion.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void doInstall ()
        throws Exception
{
    // When running without UI, we assume license is accepted
    if (!Installer.hasUI()) {
        return;
    }

    // User choice (must be an output, yet final)
    final boolean[] isOk = new boolean[1];

    final String yes = "Yes";
    final String no = "No";
    final String browse = "View License";
    final JOptionPane optionPane = new JOptionPane(
            "Do you agree to license " + LICENSE_NAME + "?",
            JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_CANCEL_OPTION,
            null,
            new Object[]{yes, no, browse},
            yes);
    final String frameTitle = "End User License Agreement";
    final JDialog dialog = new JDialog(
            Installer.getFrame(),
            frameTitle,
            true);
    dialog.setContentPane(optionPane);

    // Prevent dialog closing
    dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

    optionPane.addPropertyChangeListener(
            new PropertyChangeListener()
    {
        @Override
        public void propertyChange (PropertyChangeEvent e)
        {
            String prop = e.getPropertyName();

            if (dialog.isVisible()
                && (e.getSource() == optionPane)
                && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                Object option = optionPane.getValue();
                logger.debug("option: {}", option);

                if (option == yes) {
                    isOk[0] = true;
                    dialog.setVisible(false);
                    dialog.dispose();
                } else if (option == no) {
                    isOk[0] = false;
                    dialog.setVisible(false);
                    dialog.dispose();
                } else if (option == browse) {
                    logger.info(
                            "Launching browser on {}",
                            LICENSE_URL);
                    showLicense();
                    optionPane.setValue(
                            JOptionPane.UNINITIALIZED_VALUE);
                } else {
                }
            }
        }
    });

    dialog.pack();
    dialog.setLocationRelativeTo(Installer.getFrame());
    dialog.setVisible(true);

    logger.debug("OK: {}", isOk[0]);

    if (!isOk[0]) {
        throw new LicenseDeclinedException();
    }
}
 
Example 5
Source File: PollIntervalDialog.java    From sql-developer-keepalive with MIT License 4 votes vote down vote up
private void initDialog() {
    textField = new JTextField(20);
    PlainDocument doc = (PlainDocument) textField.getDocument();
    doc.setDocumentFilter(new NumberFilter());

    Object[] controls = { "Specify poll interval (in seconds):", textField };
    Object[] options = { OK_STRING, CANCEL_STRING };

    optionPane = new JOptionPane(controls, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);
    setContentPane(optionPane);

    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent ce) {
            textField.requestFocusInWindow();
        }
    });

    optionPane.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent e) {

            if (isVisible() && JOptionPane.VALUE_PROPERTY.equals(e.getPropertyName())) {

                Object value = optionPane.getValue();

                if (value == JOptionPane.UNINITIALIZED_VALUE) {
                    return;
                }

                switch (value.toString()) {
                case OK_STRING:
                    String v = textField.getText();
                    if (checkValue(v)) {
                        pollInterval = Integer.parseInt(v, 10);
                        setVisible(false);
                    } else {
                        //to let PropertyChangeEvent fire next time the user presses button
                        optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                    }
                    break;

                case CANCEL_STRING:
                    pollInterval = null;
                    setVisible(false);
                    break;
                }
            }
        }

    });
}
 
Example 6
Source File: InputDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
  * Prompt and return input.
  *
  * @param title       the title of the dialog.
  * @param description the dialog description.
  * @param icon        the icon to use.
  * @param parent      the parent to use.
  * @return the user input.
  */
 public String getInput(String title, String description, Icon icon, Component parent) {
     textArea = new JTextArea();
     textArea.setLineWrap(true);

     TitlePanel titlePanel = new TitlePanel(title, description, icon, true);

     // Construct main panel w/ layout.
     final JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout());
     mainPanel.add(titlePanel, BorderLayout.NORTH);

     // The user should only be able to close this dialog.
     final Object[] options = {Res.getString("ok"), Res.getString("cancel")};
     optionPane = new JOptionPane(new JScrollPane(textArea), JOptionPane.PLAIN_MESSAGE,
         JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

     mainPanel.add(optionPane, BorderLayout.CENTER);

     // Lets make sure that the dialog is modal. Cannot risk people
     // losing this dialog.
     JOptionPane p = new JOptionPane();
     dialog = p.createDialog(parent, title);
     dialog.setModal(true);
     dialog.pack();
     dialog.setSize(width, height);
     dialog.setContentPane(mainPanel);
     dialog.setLocationRelativeTo(parent);
     optionPane.addPropertyChangeListener(this);

     // Add Key Listener to Send Field
     textArea.addKeyListener(new KeyAdapter() {
         @Override
public void keyPressed(KeyEvent e) {
             if (e.getKeyChar() == KeyEvent.VK_TAB) {
                 optionPane.requestFocus();
             }
             else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                 dialog.dispose();
             }
         }
     });

     textArea.requestFocus();
     textArea.setWrapStyleWord(true);


     dialog.setVisible(true);
     return stringValue;
 }
 
Example 7
Source File: PasswordDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
  * Prompt and return input.
  *
  * @param title       the title of the dialog.
  * @param description the dialog description.
  * @param icon        the icon to use.
  * @param parent      the parent to use.
  * @return the user input.
  */
 public String getPassword(String title, String description, Icon icon, Component parent) {
     passwordField = new JPasswordField();
     passwordField.setText(password);

     TitlePanel titlePanel = new TitlePanel(title, description, icon, true);

     // Construct main panel w/ layout.
     final JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout());
     mainPanel.add(titlePanel, BorderLayout.NORTH);

     final JPanel passwordPanel = new JPanel(new GridBagLayout());
     JLabel passwordLabel = new JLabel(Res.getString("label.enter.password") + ":");
     passwordPanel.add(passwordLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
     passwordPanel.add(passwordField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

     //user should be able to close this dialog (with an option to save room's password)
     passwordPanel.add(_savePasswordBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
     ResourceUtils.resButton(_savePasswordBox, Res.getString("checkbox.save.password"));
     final Object[] options = {Res.getString("ok"), Res.getString("cancel") , };
     optionPane = new JOptionPane(passwordPanel, JOptionPane.PLAIN_MESSAGE,
         JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

     mainPanel.add(optionPane, BorderLayout.CENTER);

     // Lets make sure that the dialog is modal. Cannot risk people
     // losing this dialog.
     JOptionPane p = new JOptionPane();
     dialog = p.createDialog(parent, title);
     dialog.setModal(true);
     dialog.pack();
     dialog.setSize(width, height);
     dialog.setContentPane(mainPanel);
     dialog.setLocationRelativeTo(parent);
     optionPane.addPropertyChangeListener(this);

     // Add Key Listener to Send Field
     passwordField.addKeyListener(new KeyAdapter() {
         @Override
public void keyPressed(KeyEvent e) {
             if (e.getKeyChar() == KeyEvent.VK_TAB) {
                 optionPane.requestFocus();
             }
             else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                 dialog.dispose();
             }
         }
     });

     passwordField.requestFocus();


     dialog.setVisible(true);
     return stringValue;
 }
 
Example 8
Source File: InputTextAreaDialog.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
  * Prompt and return input.
  *
  * @param title       the title of the dialog.
  * @param description the dialog description.
  * @param icon        the icon to use.
  * @param parent      the parent to use.
  * @return the user input.
  */
 public String getInput(String title, String description, Icon icon, Component parent) {
     textArea = new JTextArea();
     textArea.setLineWrap(true);

     TitlePanel titlePanel = new TitlePanel(title, description, icon, true);

     // Construct main panel w/ layout.
     final JPanel mainPanel = new JPanel();
     mainPanel.setLayout(new BorderLayout());
     mainPanel.add(titlePanel, BorderLayout.NORTH);

     // The user should only be able to close this dialog.
     final Object[] options = {Res.getString("ok"), Res.getString("cancel")};
     optionPane = new JOptionPane(new JScrollPane(textArea), JOptionPane.PLAIN_MESSAGE,
             JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);

     mainPanel.add(optionPane, BorderLayout.CENTER);

     // Let's make sure that the dialog is modal. Cannot risk people
     // losing this dialog.
     JOptionPane p = new JOptionPane();
     dialog = p.createDialog(parent, title);
     dialog.setModal(true);
     dialog.pack();
     dialog.setSize(width, height);
     dialog.setContentPane(mainPanel);
     dialog.setLocationRelativeTo(parent);
     optionPane.addPropertyChangeListener(this);

     // Add Key Listener to Send Field
     textArea.addKeyListener(new KeyAdapter() {
         @Override
public void keyPressed(KeyEvent e) {
             if (e.getKeyChar() == KeyEvent.VK_TAB) {
                 optionPane.requestFocus();
             }
             else if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                 dialog.dispose();
             }
         }
     });

     textArea.requestFocus();


     dialog.setVisible(true);
     return stringValue;
 }
 
Example 9
Source File: VCardEditor.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Displays a users profile.
    * 
    * @param jid
    *            the jid of the user.
    * @param vcard
    *            the users vcard.
    * @param parent
    *            the parent component, used for location handling.
    */
   public void displayProfile(final BareJid jid, VCard vcard, JComponent parent) {
VCardViewer viewer = new VCardViewer(jid);

final JFrame dlg = new JFrame(Res.getString("title.view.profile.for",
	jid));

avatarLabel = new JLabel();
avatarLabel.setHorizontalAlignment(JButton.RIGHT);
avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white,
	Color.lightGray));

// The user should only be able to close this dialog.
Object[] options = { Res.getString("button.view.profile"),
	Res.getString("close") };
final JOptionPane pane = new JOptionPane(viewer,
	JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null,
	options, options[0]);

// mainPanel.add(pane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
// GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5,
// 5, 5), 0, 0));

dlg.setIconImage(SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16)
	.getImage());

dlg.pack();
dlg.setSize(350, 250);
dlg.setResizable(true);
dlg.setContentPane(pane);
dlg.setLocationRelativeTo(parent);

PropertyChangeListener changeListener = new PropertyChangeListener() {
    @Override
	public void propertyChange(PropertyChangeEvent e) {
	if (pane.getValue() instanceof Integer) {
	    pane.removePropertyChangeListener(this);
	    dlg.dispose();
	    return;
	}
	String value = (String) pane.getValue();
	if (Res.getString("close").equals(value)) {
	    pane.removePropertyChangeListener(this);
	    dlg.dispose();
	} else if (Res.getString("button.view.profile").equals(value)) {
	    pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
	    SparkManager.getVCardManager().viewFullProfile(jid, pane);
	}
    }
};

pane.addPropertyChangeListener(changeListener);

dlg.addKeyListener(new KeyAdapter() {
    @Override
	public void keyPressed(KeyEvent keyEvent) {
	if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) {
	    dlg.dispose();
	}
    }
});

dlg.setVisible(true);
dlg.toFront();
dlg.requestFocus();
   }