Example #1
0
  /**
   * Utility method to put the given panel in a JDialog. You'll have to show it on your own.
   *
   * @param panel the panel to put in a dialog
   * @param owner the JFrame that owns the dialog. You can pass null.
   * @param dialogTitle the title for the dialog
   * @param iconPath the path to the dialog's icon, such as foo.png
   * @param width the dialog's width. Pass -1 to pack().
   * @param height the dialog's height. Pass -1 to pack().
   * @return the JDialog the panel is in
   */
  public static JDialog putPanelInDialog(
      JPanel panel, JFrame owner, String dialogTitle, String iconPath, int width, int height) {
    JDialog dialog = new JDialog(owner, dialogTitle, true); // boolean means modality

    dialog.add(panel);
    dialog.setIconImage(ImageManager.createImageIcon(iconPath).getImage());
    if (width < 0 && height < 0) dialog.pack();
    else dialog.setSize(width, height);
    dialog.setResizable(false);
    if (owner != null) Utils.centerComponent(dialog, owner);
    else dialog.setLocationRelativeTo(null);

    return dialog;
  }
Example #2
0
  /**
   * Centers the given component on the user's screen.
   *
   * @param component a component (usually a frame.)
   */
  public static void centerOnScreen(Component component) {
    Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    int screenWidth = (int) screenSize.getWidth();
    int screenHeight = (int) screenSize.getHeight();

    int componentWidth = component.getWidth();
    int componentHeight = component.getHeight();

    int top = (screenHeight - componentHeight) / 2;
    int left = (screenWidth - componentWidth) / 2;

    Utils.changeFrameLocation(component, left, top);
  }
Example #3
0
  /**
   * Centers the given component in relation to its owner.
   *
   * @param component the component to center
   * @param owner the parent frame
   */
  public static void centerComponent(Component component, Component owner) {
    // find the difference in width to see the offsets
    int widthDifference = owner.getWidth() - component.getWidth();
    int heightDifference = owner.getHeight() - component.getHeight();

    // we can divide the differences by 2 and add that to the owner's top left
    // and then make that the top left of the component
    // to center the frame
    int leftOffset = widthDifference / 2;
    int topOffset = heightDifference / 2;

    // these are the new locations
    int left = owner.getX() + leftOffset;
    int top = owner.getY() + topOffset;

    Utils.changeFrameLocation(component, left, top);
  }
Example #4
0
  /**
   * Uses a JEditorPane to open the given URL in a dialog
   *
   * @param url the url to load
   * @param title the title of the dialog
   * @param frame the parent frame
   * @param visible true if you want the dialog to be visible, false if you want it to be invisible
   *     to user
   * @param iconPath the path to the image icon for the dialog.
   */
  public static void openURLinDialog(
      String url, String title, String iconPath, JFrame frame, boolean visible) {
    // create panel with page in it
    final int WIDTH = 400;
    final int HEIGHT = 300;
    JPanel webView = createEditorPane(url, WIDTH, HEIGHT);

    // put it in a dialog
    JDialog dialog = new JDialog(frame);
    dialog.setTitle(title);
    dialog.setIconImage(GUI.createImageIcon(iconPath).getImage());
    dialog.setContentPane(webView);
    dialog.pack();
    dialog.setResizable(false);
    dialog.setModal(true);
    Utils.centerComponent(dialog, frame);

    // open it
    dialog.setVisible(visible);
  }