Example usage for javax.swing JDialog getOwner

List of usage examples for javax.swing JDialog getOwner

Introduction

In this page you can find the example usage for javax.swing JDialog getOwner.

Prototype

public Window getOwner() 

Source Link

Document

Returns the owner of this window.

Usage

From source file:jatoo.proxy.dialog.ProxyDialog.java

/**
 * Shows the dialog relative to the specified owner.
 *//*from  w  ww  .  j a  va 2s . c  om*/
public static synchronized void show(Component owner) {

    JDialog dialogTmp;

    if (owner == null) {
        dialogTmp = new JDialog();
    } else {
        dialogTmp = new JDialog(SwingUtilities.getWindowAncestor(owner));
    }

    final JDialog dialog = dialogTmp;

    //
    // the panel

    final ProxyDialogPanel dialogPanel = PROXY_DIALOG_PANEL_FACTORY.createDialogPanel();

    try {

        Proxy proxy = new Proxy();
        proxy.load();

        dialogPanel.setProxyEnabled(proxy.isEnabled());
        dialogPanel.setHost(proxy.getHost());
        dialogPanel.setPort(proxy.getPort());
        dialogPanel.setProxyRequiringAuthentication(proxy.isRequiringAuthentication());
        dialogPanel.setUsername(proxy.getUsername());
        dialogPanel.setPassword(proxy.getPassword());
    }

    catch (FileNotFoundException e) {
        // do nothing, maybe is the first time and the file is missing
    }

    catch (Exception e) {
        logger.error("Failed to load the properties.", e);
    }

    //
    // buttons

    JButton okButton = new JButton("Ok");
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {

            try {

                if (dialogPanel.isProxyEnabled()) {

                    if (dialogPanel.isProxyRequiringAuthentication()) {
                        ProxyUtils.setProxy(dialogPanel.getHost(), dialogPanel.getPort(),
                                dialogPanel.getUsername(), dialogPanel.getPassword());
                    } else {
                        ProxyUtils.setProxy(dialogPanel.getHost(), dialogPanel.getPort());
                    }
                }

                else {
                    ProxyUtils.removeProxy();
                }

                dialog.dispose();
            }

            catch (Exception e) {
                JOptionPane.showMessageDialog(dialog, "Failed to set the proxy:\n" + e.toString());
                return;
            }

            try {

                Proxy proxy = new Proxy();

                proxy.setEnabled(dialogPanel.isProxyEnabled());
                proxy.setUsername(dialogPanel.getUsername());
                proxy.setPassword(dialogPanel.getPassword());
                proxy.setRequiringAuthentication(dialogPanel.isProxyRequiringAuthentication());
                proxy.setHost(dialogPanel.getHost());
                proxy.setPort(dialogPanel.getPort());

                proxy.store();
            }

            catch (Exception e) {
                logger.error("Failed to save the properties.", e);
            }
        }
    });

    JButton cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    });

    //
    // layout dialog

    dialogPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    JPanel buttonsGroup = new JPanel(new GridLayout(1, 2, 5, 5));
    buttonsGroup.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    buttonsGroup.add(okButton);
    buttonsGroup.add(cancelButton);

    JPanel buttonsPanel = new JPanel(new BorderLayout());
    buttonsPanel.add(buttonsGroup, BorderLayout.LINE_END);

    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.add(dialogPanel, BorderLayout.CENTER);
    contentPane.add(buttonsPanel, BorderLayout.PAGE_END);

    //
    // setup dialog

    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setTitle("Proxy Settings");
    dialog.setContentPane(contentPane);
    dialog.pack();
    dialog.setLocationRelativeTo(dialog.getOwner());
    dialog.setModal(true);

    //
    // and show

    dialog.setVisible(true);
}

From source file:fxts.stations.util.preferences.EditAction.java

public void actionPerformed(ActionEvent aEvent) {
    JButton okButton = UIManager.getInst().createButton();
    JButton cancelButton = UIManager.getInst().createButton();
    final JDialog dialog = new JDialog(mEditorPanel.getParentDialog());
    dialog.setTitle(mEditorPanel.getTitle());
    JPanel editPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    //mainPanel.setLayout(new SideLayout());

    //sets button panel
    buttonPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    okButton.setText(mResMan.getString("IDS_OK_BUTTON"));
    //okButton.setPreferredSize(new Dimension(80, 27));
    GridBagConstraints sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 0;//from  www  .ja  v  a 2 s.c om
    sideConstraints.gridy = 0;
    ResizeParameterWrapper resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.5, 0.0, 0.5, 0.0);
    resizeParameter.setToConstraints(sideConstraints);
    buttonPanel.add(okButton, sideConstraints);
    cancelButton.setText(mResMan.getString("IDS_CANCEL_BUTTON"));
    //cancelButton.setPreferredSize(new Dimension(80, 27));
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 1;
    sideConstraints.gridy = 0;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.5, 0.0, 0.5, 0.0);
    resizeParameter.setToConstraints(sideConstraints);
    buttonPanel.add(cancelButton, sideConstraints);

    //adds button panel
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.insets = new Insets(10, 10, 10, 10);
    sideConstraints.gridx = 0;
    sideConstraints.gridy = 1;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 1.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    mainPanel.add(buttonPanel, sideConstraints);

    //sets edit panel
    final IEditor editor = mType.getEditor();
    editor.setValue(mValue);
    editPanel.setLayout(UIFrontEnd.getInstance().getSideLayout());
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    Component editComp = editor.getComponent();

    //Mar 25 2004 - kav: added for right tab order at Font Chooser at java 1.4.
    if (editComp instanceof FontChooser) {
        FontChooser fc = (FontChooser) editComp;
        fc.setNextFocusedComp(okButton);
    }
    editPanel.add(editComp, sideConstraints);

    //adds editor panel
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.gridx = 0;
    sideConstraints.gridy = 0;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    mainPanel.add(editPanel, sideConstraints);

    //adds main panel
    dialog.getContentPane().setLayout(UIFrontEnd.getInstance().getSideLayout());
    //dialog.getContentPane().setLayout(new SideLayout());
    sideConstraints = UIFrontEnd.getInstance().getSideConstraints();
    sideConstraints.fill = GridBagConstraints.BOTH;
    resizeParameter = UIFrontEnd.getInstance().getResizeParameter();
    resizeParameter.init(0.0, 0.0, 1.0, 1.0);
    resizeParameter.setToConstraints(sideConstraints);
    dialog.getContentPane().add(mainPanel, sideConstraints);

    //adds listeners to buttons
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aEvent) {
            if (editor.getValue().equals(mValue)) {
                //
            } else {
                mValue = editor.getValue();
                mEditorPanel.setValue(mValue);
                mEditorPanel.refreshControls();
                mEditorPanel.setValueChanged(true);
            }
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            "ExitAction");
    okButton.getActionMap().put("ExitAction", new AbstractAction() {
        /**
         * Invoked when an action occurs.
         */
        public void actionPerformed(ActionEvent aEvent) {
            editor.setValue(mValue);
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    okButton.requestFocus();
    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent aEvent) {
            editor.setValue(mValue);
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    //dialog.setResizable(false);
    dialog.setModal(true);
    dialog.pack();

    //sets minimal sizes for components
    Dimension dim = mainPanel.getSize();
    mainPanel.setMinimumSize(dim);
    mainPanel.setPreferredSize(dim);

    //sets size of buttons
    Dimension dimOkButton = okButton.getSize();
    Dimension dimCancelButton = cancelButton.getSize();
    int nMaxWidth = dimOkButton.getWidth() > dimCancelButton.getWidth() ? (int) dimOkButton.getWidth()
            : (int) dimCancelButton.getWidth();
    okButton.setPreferredSize(new Dimension(nMaxWidth, (int) dimOkButton.getHeight()));
    okButton.setSize(new Dimension(nMaxWidth, (int) dimOkButton.getHeight()));
    cancelButton.setPreferredSize(new Dimension(nMaxWidth, (int) dimCancelButton.getHeight()));
    cancelButton.setSize(new Dimension(nMaxWidth, (int) dimCancelButton.getHeight()));
    dialog.setLocationRelativeTo(dialog.getOwner());
    dialog.setVisible(true);
}