Пример #1
0
  /**
   * Brings up a sheet with a specified icon, where the initial choice is determined by the {@code
   * initialValue} parameter and the number of choices is determined by the {@code optionType}
   * parameter.
   *
   * <p>If {@code optionType} is {@code YES_NO_OPTION}, or {@code YES_NO_CANCEL_OPTION} and the
   * {@code options} parameter is {@code null}, then the options are supplied by the look and feel.
   *
   * <p>The {@code messageType} parameter is primarily used to supply a default icon from the look
   * and feel.
   *
   * @param parentComponent determines the {@code Frame} in which the dialog is displayed; if {@code
   *     null}, or if the {@code parentComponent} has no {@code Frame}, the sheet is displayed as a
   *     dialog.
   * @param message the {@code Object} to display
   * @param optionType an integer designating the options available on the dialog: {@code
   *     YES_NO_OPTION}, or {@code YES_NO_CANCEL_OPTION}
   * @param messageType an integer designating the kind of message this is, primarily used to
   *     determine the icon from the pluggable Look and Feel: {@code JOptionPane.ERROR_MESSAGE},
   *     {@code JOptionPane.INFORMATION_MESSAGE}, {@code JOptionPane.WARNING_MESSAGE}, {@code
   *     JOptionPane.QUESTION_MESSAGE}, or {@code JOptionPane.PLAIN_MESSAGE}
   * @param icon the icon to display in the dialog
   * @param options an array of objects indicating the possible choices the user can make; if the
   *     objects are components, they are rendered properly; non-{@code String} objects are rendered
   *     using their {@code toString} methods; if this parameter is {@code null}, the options are
   *     determined by the Look and Feel
   * @param initialValue the object that represents the default selection for the dialog; only
   *     meaningful if {@code options} is used; can be {@code null}
   * @param listener The listener for SheetEvents.
   */
  public static void showOptionSheet(
      Component parentComponent,
      Object message,
      int optionType,
      int messageType,
      Icon icon,
      Object[] options,
      Object initialValue,
      SheetListener listener) {

    JOptionPane pane =
        new JOptionPane(message, messageType, optionType, icon, options, initialValue);

    pane.setInitialValue(initialValue);
    pane.setComponentOrientation(
        ((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent)
            .getComponentOrientation());

    int style = styleFromMessageType(messageType);
    JSheet sheet = createSheet(pane, parentComponent, style);
    pane.selectInitialValue();
    sheet.addSheetListener(listener);
    sheet.show();
    sheet.toFront();
  }
Пример #2
0
 /**
  * Displays an option pane as a sheet on its parent window.
  *
  * @param pane The option pane.
  * @param parentComponent The parent of the option pane.
  * @param listener The listener for SheetEvents.
  */
 public static void showSheet(
     JOptionPane pane, Component parentComponent, SheetListener listener) {
   final JSheet sheet =
       createSheet(pane, parentComponent, styleFromMessageType(pane.getMessageType()));
   sheet.addSheetListener(listener);
   sheet.show();
 }
Пример #3
0
  /**
   * Prompts the user for input in a sheet where the initial selection, possible selections, and all
   * other options can be specified. The user will able to choose from {@code selectionValues},
   * where {@code null} implies the user can input whatever they wish, usually by means of a {@code
   * JTextField}. {@code initialSelectionValue} is the initial value to prompt the user with. It is
   * up to the UI to decide how best to represent the {@code selectionValues}, but usually a {@code
   * JComboBox}, {@code JList}, or {@code JTextField} will be used.
   *
   * @param parentComponent the parent {@code Component} for the dialog
   * @param message the {@code Object} to display
   * @param messageType the type of message to be displayed: {@code JOptionPane.ERROR_MESSAGE},
   *     {@code JOptionPane.INFORMATION_MESSAGE}, {@code JOptionPane.WARNING_MESSAGE}, {@code
   *     JOptionPane.QUESTION_MESSAGE}, or {@code JOptionPane.PLAIN_MESSAGE}
   * @param icon the {@code Icon} image to display
   * @param selectionValues an array of {@code Object}s that gives the possible selections
   * @param initialSelectionValue the value used to initialize the input field
   * @param listener The listener for SheetEvents.
   */
  public static void showInputSheet(
      Component parentComponent,
      Object message,
      int messageType,
      Icon icon,
      Object[] selectionValues,
      Object initialSelectionValue,
      SheetListener listener) {

    JOptionPane pane =
        new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION, icon, null, null);

    pane.setWantsInput(true);
    pane.setSelectionValues(selectionValues);
    pane.setInitialSelectionValue(initialSelectionValue);
    pane.setComponentOrientation(
        ((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent)
            .getComponentOrientation());

    int style = styleFromMessageType(messageType);
    JSheet sheet = createSheet(pane, parentComponent, style);

    pane.selectInitialValue();

    /*
    sheet.addWindowListener(new WindowAdapter() {
    public void windowClosed(WindowEvent evt) {
    sheet.dispose();
    }
    });*/
    sheet.addSheetListener(listener);
    sheet.show();
    sheet.toFront();
  }
Пример #4
0
  /**
   * Displays a custom file chooser sheet with a custom approve button.
   *
   * @param chooser the chooser
   * @param parent the parent component of the dialog; can be {@code null}
   * @param approveButtonText the text of the {@code ApproveButton}
   * @param listener The listener for SheetEvents.
   */
  public static void showSheet(
      final JFileChooser chooser,
      Component parent,
      String approveButtonText,
      SheetListener listener) {
    if (approveButtonText != null) {
      chooser.setApproveButtonText(approveButtonText);
      chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
    }

    // Begin Create Dialog
    Frame frame =
        parent instanceof Frame
            ? (Frame) parent
            : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);

    String title = chooser.getUI().getDialogTitle(chooser);
    chooser.getAccessibleContext().setAccessibleDescription(title);

    final JSheet sheet = new JSheet(frame);
    sheet.addSheetListener(listener);

    Container contentPane = sheet.getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(chooser, BorderLayout.CENTER);
    // End Create Dialog

    final ActionListener actionListener =
        new ActionListener() {

          public void actionPerformed(ActionEvent evt) {
            int option;
            if (evt.getActionCommand().equals("ApproveSelection")) {
              option = JFileChooser.APPROVE_OPTION;
            } else {
              option = JFileChooser.CANCEL_OPTION;
            }
            sheet.hide();
            sheet.fireOptionSelected(chooser, option);
            chooser.removeActionListener(this);
          }
        };
    chooser.addActionListener(actionListener);
    sheet.addWindowListener(
        new WindowAdapter() {

          @Override
          public void windowClosing(WindowEvent e) {
            sheet.fireOptionSelected(chooser, JFileChooser.CANCEL_OPTION);
            chooser.removeActionListener(actionListener);
          }
        });
    chooser.rescanCurrentDirectory();
    sheet.pack();
    sheet.show();
    sheet.toFront();
  }