Example usage for javax.swing SwingUtilities getWindowAncestor

List of usage examples for javax.swing SwingUtilities getWindowAncestor

Introduction

In this page you can find the example usage for javax.swing SwingUtilities getWindowAncestor.

Prototype

public static Window getWindowAncestor(Component c) 

Source Link

Document

Returns the first Window ancestor of c, or null if c is not contained inside a Window.

Usage

From source file:Main.java

/**
 * Gets the parent JFrame of the component.
 *///from  w w  w  . j a v  a  2 s.  co m
public static JFrame getJFrame(Component cmp) {
    return (JFrame) SwingUtilities.getWindowAncestor(cmp);
}

From source file:Main.java

public static void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {/*from w  w w . j a  va2 s. c o m*/
        source.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                    SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
                }
            }
        });
    }
}

From source file:Main.java

/**
 * Get the window the given component is located in.
 *
 * @param component Component.//w w w. ja v  a 2 s  .c  om
 *
 * @return Window.
 */
public static Window getComponentWindow(Component component) {
    return SwingUtilities.getWindowAncestor(component);
}

From source file:Main.java

/**
 * Tries to find the owning window for the given component.
 * /*  w  w w  .j a  v a 2  s. c o m*/
 * @param aComponent
 *          the AWT event to find the owning window for, may be
 *          <code>null</code>.
 * @return the owning window, or <code>null</code> if no such window could be
 *         found, or a <code>null</code> component was given.
 */
public static final Window getOwningWindow(final Component aComponent) {
    if (aComponent == null) {
        return null;
    }

    Window owner = SwingUtilities.getWindowAncestor(aComponent);
    if (owner == null) {
        owner = getCurrentWindow();
    }
    return owner;
}

From source file:Main.java

public Main() {
    comboBox = new JComboBox(new String[] { "Select Pet", "Bird", "Cat", "Dog", "Rabbit", "Pig", "Other" });
    add(comboBox, BorderLayout.PAGE_START);
    JFrame frame = new JFrame("Main");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comboBox);/*from  w w  w.j av  a 2  s .c  o m*/
    frame.pack();
    frame.setVisible(true);

    comboBox.showPopup();
    Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
    BasicComboPopup popup = (BasicComboPopup) child;
    popup.setName("BasicComboPopup");
    JList list = popup.getList();
    Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, list);
    JScrollPane scrollPane = (JScrollPane) c;

    Window mainFrame = SwingUtilities.windowForComponent(comboBox);

    System.out.println(mainFrame.getName());
    Window popupWindow = SwingUtilities.windowForComponent(popup);
    System.out.println(popupWindow);
    Window popupWindowa = SwingUtilities.windowForComponent(c);
    System.out.println(popupWindowa);

    Window mainFrame1 = SwingUtilities.getWindowAncestor(comboBox);

    System.out.println(mainFrame1);
    Window popupWindow1 = SwingUtilities.getWindowAncestor(popup);
    System.out.println(popupWindow1);

    Component mainFrame2 = SwingUtilities.getRoot(comboBox);

    System.out.println(mainFrame2.getName());
    Component popupWindow2 = SwingUtilities.getRoot(popup);
    System.out.println(popupWindow2);

    if (popupWindow != mainFrame) {
        popupWindow.pack();
    }
}

From source file:eu.delving.sip.actions.ImportAction.java

public ImportAction(JDesktopPane parent, SipModel sipModel) {
    configAction(this, "Import new source data", ICON_IMPORT, MENU_I);
    this.parent = parent;
    this.sipModel = sipModel;
    this.dialog = new JDialog(SwingUtilities.getWindowAncestor(parent), "Input Source",
            Dialog.ModalityType.APPLICATION_MODAL);
    setEnabled(false);//from   w w w .  j a va 2  s  .c o  m
    prepareDialog();
    prepareChooser(sipModel);
    sipModel.getDataSetModel().addListener(new DataSetModel.SwingListener() {
        @Override
        public void stateChanged(DataSetModel model, DataSetState state) {
            setEnabled(state != ABSENT);
        }
    });
}

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

/**
 * Shows the dialog relative to the specified owner.
 *///from  w  w w  .j  a va2s .c  o m
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:aurelienribon.gdxsetupui.ui.panels.ConfigUpdatePanel.java

private void browse() {
    String path = Ctx.cfgUpdate.destinationPath;
    JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);

    JFileChooser chooser = new JFileChooser(new File(path));
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setDialogTitle("Select the core project folder");

    if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
        pathField.setText(chooser.getSelectedFile().getPath());
        updateConfig(chooser.getSelectedFile());
        updatePanel();//w  ww . java2 s .  c  o  m
    }
}

From source file:com.opendoorlogistics.components.reports.ReporterPanel.java

public ReporterPanel(final ComponentConfigurationEditorAPI api, final ReporterConfig config) {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // setAlignmentX(LEFT_ALIGNMENT);

    // add config panel
    ReporterConfigPanel configPanel = new ReporterConfigPanel(config);
    configPanel.setBorder(createBorder("Export and processing options"));
    add(configPanel);//from  w  w  w  .  j a  v a  2s . c o m

    // add gap
    add(Box.createRigidArea(new Dimension(1, 10)));

    // add tools panel
    JPanel toolContainer = new JPanel();
    toolContainer.setLayout(new BorderLayout());
    toolContainer.setBorder(createBorder("Tools"));

    add(toolContainer);

    JPanel tools = new JPanel();
    toolContainer.add(tools, BorderLayout.NORTH);
    toolContainer.setMaximumSize(new Dimension(Integer.MAX_VALUE, api.isInstruction() ? 120 : 80));
    // tools.setLayout(new BoxLayout(tools, BoxLayout.X_AXIS));
    tools.setLayout(new GridLayout(api == null || api.isInstruction() ? 2 : 1, 3));
    JButton compileButton = new JButton("Compile .jrxml file");
    compileButton.setToolTipText("Compile a JasperReports .jrxml file");
    compileButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            File file = ReporterTools.chooseJRXMLFile(api.getComponentPreferences(), LAST_JRXML_TO_COMPILE,
                    ReporterPanel.this);
            if (file == null) {
                return;
            }

            final ExecutionReport report = api.getApi().uiFactory().createExecutionReport();
            try {
                JasperDesign design = JRXmlLoader.load(file);
                if (design == null) {
                    throw new RuntimeException("File to load jrxml: " + file.getAbsolutePath());
                }

                String filename = FilenameUtils.removeExtension(file.getAbsolutePath()) + ".jasper";
                JasperCompileManager.compileReportToFile(design, filename);
            } catch (Throwable e2) {
                report.setFailed(e2);
                report.setFailed("Failed to compile file " + file.getAbsolutePath());
            } finally {
                if (report.isFailed()) {
                    Window window = SwingUtilities.getWindowAncestor(ReporterPanel.this);
                    api.getApi().uiFactory()
                            .createExecutionReportDialog(
                                    JFrame.class.isInstance(window) ? (JFrame) window : null,
                                    "Compiling jrxml file", report, true)
                            .setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(ReporterPanel.this,
                            "Compiled jxrml successfully: " + file.getAbsolutePath());
                }
            }
        }
    });
    tools.add(compileButton);

    for (final OrientationEnum orientation : new OrientationEnum[] { OrientationEnum.LANDSCAPE,
            OrientationEnum.PORTRAIT }) {
        // create export button
        JButton button = new JButton("Export " + orientation.getName().toLowerCase() + " template");
        button.setToolTipText(
                "Export template (editable .jrxml and compiled .jasper) based on the input tables ("
                        + orientation.getName().toLowerCase() + ")");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ReporterTools.exportReportTemplate(api, config, orientation, ReporterPanel.this);
            }
        });
        tools.add(button);

        // create view button
        if (api.isInstruction()) {
            final String title = "View basic " + orientation.getName().toLowerCase() + " report";
            button = new JButton(title);
            button.setToolTipText("View basic report based on the input tables ("
                    + orientation.getName().toLowerCase() + ")");
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (api != null) {
                        api.executeInPlace(title,
                                orientation == OrientationEnum.LANDSCAPE
                                        ? ReporterComponent.VIEW_BASIC_LANDSCAPE
                                        : ReporterComponent.VIEW_BASIC_PORTRAIT);
                    }
                }
            });
            tools.add(button);
        }
    }

}

From source file:com.opendoorlogistics.utils.image.ExportImagePanel.java

public static ExportImageConfig showModal(Component parent, ExportImageConfig inputConfig) {
    return showModal(parent != null ? SwingUtilities.getWindowAncestor(parent) : null, parent, inputConfig);
}