Example usage for javax.swing SwingUtilities getRoot

List of usage examples for javax.swing SwingUtilities getRoot

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public static Component getRoot(Component c) 

Source Link

Document

Returns the root component for the current component tree.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create an action
    Action action = new AbstractAction("Action Label") {
        // This method is called when the action is triggered
        public void actionPerformed(ActionEvent evt) {
            Component c = (Component) evt.getSource();

            // Get the frame
            Component frame = SwingUtilities.getRoot(c);

            // Hide the frame
            frame.setVisible(false);/*from w  w w  .j a v  a2  s.co m*/
        }
    };

}

From source file:Main.java

/**
 * Tests if 2 components - a and b, desecents from the same root
 * /*w w  w.j  a v a2s  .  c  o m*/
 * @param a
 * @param b
 * @return
 */
public static boolean DesecendsFromSameFrame(Component a, Component b) {
    return SwingUtilities.getRoot(a) == SwingUtilities.getRoot(b);
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.setBackground(Color.RED);
        }/*www.j a va 2  s. c  o m*/
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.getContentPane().setBackground(Color.RED);
        }/*from  w ww  .j  ava  2s  .  com*/
    });
    this.getContentPane().add(button);
}

From source file:gda.plots.DataMagnifierWindow.java

/**
 * @param c
 */
public DataMagnifierWindow(Component c) {
    super((Frame) SwingUtilities.getRoot(c));
    setAlwaysOnTop(true);
}

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.jav  a2s  .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:com.haulmont.cuba.desktop.gui.components.DesktopWindow.java

public DesktopWindow() {
    initLayout();/*from w w w . ja  v a 2s.com*/
    delegate = createDelegate();
    actionsHolder = new DesktopFrameActionsHolder(this, panel);

    setWidth("100%");

    panel.addAncestorListener(new AncestorListener() {
        @Override
        public void ancestorAdded(AncestorEvent event) {
            SwingUtilities.invokeLater(() -> {
                if (!isAttachedToRoot) {
                    if (SwingUtilities.getRoot(event.getComponent()) != null) {
                        enableEventListeners();
                        isAttachedToRoot = true;
                    }
                }
            });
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            SwingUtilities.invokeLater(() -> {
                if (isAttachedToRoot) {
                    if (SwingUtilities.getRoot(event.getComponent()) == null) {
                        disableEventListeners();
                        isAttachedToRoot = false;
                    }
                }
            });
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
            // do nothing
        }
    });
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void initComponent() {
    layout = new MigLayout("flowy, fill, insets 0", "", "[min!][fill]");
    panel = new JPanel(layout);

    topPanel = new JPanel(new BorderLayout());
    topPanel.setVisible(false);/*from w w  w .  j a va2  s.co  m*/
    panel.add(topPanel, "growx");

    scrollPane = new JScrollPane(impl);
    impl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    impl.setFillsViewportHeight(true);
    panel.add(scrollPane, "grow");

    impl.setShowGrid(true);
    impl.setGridColor(Color.lightGray);

    impl.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                handleClickAction();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            showPopup(e);
        }

        protected void showPopup(MouseEvent e) {
            if (e.isPopupTrigger() && contextMenuEnabled) {
                // select row
                Point p = e.getPoint();
                int viewRowIndex = impl.rowAtPoint(p);

                int rowNumber;
                if (viewRowIndex >= 0) {
                    rowNumber = impl.convertRowIndexToModel(viewRowIndex);
                } else {
                    rowNumber = -1;
                }
                ListSelectionModel model = impl.getSelectionModel();

                if (!model.isSelectedIndex(rowNumber)) {
                    model.setSelectionInterval(rowNumber, rowNumber);
                }

                // show popup menu
                JPopupMenu popupMenu = createPopupMenu();
                if (popupMenu.getComponentCount() > 0) {
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        }
    });

    ColumnControlButton columnControlButton = new ColumnControlButton(impl) {
        @Override
        protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column) {
            ColumnVisibilityAction columnVisibilityAction = super.createColumnVisibilityAction(column);

            columnVisibilityAction.addPropertyChangeListener(evt -> {
                if ("SwingSelectedKey".equals(evt.getPropertyName()) && evt.getNewValue() instanceof Boolean) {
                    ColumnVisibilityAction action = (ColumnVisibilityAction) evt.getSource();

                    String columnName = action.getActionCommand();
                    boolean collapsed = !((boolean) evt.getNewValue());

                    Column col = getColumn(columnName);
                    if (col != null) {
                        col.setCollapsed(collapsed);
                    }
                }
            });

            return columnVisibilityAction;
        }
    };
    impl.setColumnControl(columnControlButton);

    impl.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter");
    impl.getActionMap().put("enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (enterPressAction != null) {
                enterPressAction.actionPerform(DesktopAbstractTable.this);
            } else {
                handleClickAction();
            }
        }
    });

    Messages messages = AppBeans.get(Messages.NAME);
    // localize default column control actions
    for (Object actionKey : impl.getActionMap().allKeys()) {
        if ("column.packAll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packAll"));
        } else if ("column.packSelected".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.packSelected"));
        } else if ("column.horizontalScroll".equals(actionKey)) {
            BoundAction action = (BoundAction) impl.getActionMap().get(actionKey);
            action.setName(messages.getMessage(DesktopTable.class, "DesktopTable.horizontalScroll"));
        }
    }

    // Ability to configure fonts in table
    // Add action to column control
    String configureFontsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.configureFontsLabel");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "fonts",
            new AbstractAction(configureFontsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Component rootComponent = SwingUtilities.getRoot(impl);
                    final FontDialog fontDialog = FontDialog.show(rootComponent, impl.getFont());
                    fontDialog.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosed(WindowEvent e) {
                            Font result = fontDialog.getResult();
                            if (result != null) {
                                impl.setFont(result);
                                packRows();
                            }
                        }
                    });
                    fontDialog.open();
                }
            });

    // Ability to reset settings
    String resetSettingsLabel = messages.getMessage(DesktopTable.class, "DesktopTable.resetSettings");
    impl.getActionMap().put(ColumnControlButton.COLUMN_CONTROL_MARKER + "resetSettings",
            new AbstractAction(resetSettingsLabel) {
                @Override
                public void actionPerformed(ActionEvent e) {
                    resetPresentation();
                }
            });

    scrollPane.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            if (!columnsInitialized) {
                adjustColumnHeaders();
            }
            columnsInitialized = true;
        }
    });

    // init default row height
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            if (!fontInitialized) {
                applyFont(impl, impl.getFont());
            }
        }
    });
}

From source file:com.app.model.m3.M3UpgradModel.java

public void saveas() {
    File[] f = zio.ToolsFile.fileChoice(SwingUtilities.getRoot(Ressource.mainView.getWindow()), null,
            new ArrayList(Arrays.asList(extension)));
    if (f != null) {
        if (f.length > 0) {
            filePath = f[0].getAbsolutePath();
            if (!FilenameUtils.isExtension(filePath, extension)) {
                filePath += extension[0];
            }/*w  ww.j a  va  2  s . c o m*/
            setChanged();
            notifyObservers("title");
            save(filePath);
        }
    }
}

From source file:net.schweerelos.parrot.ui.GraphViewComponent.java

@Override
public void setModel(ParrotModel model) {
    removeAll();/*from w w w . j av a 2  s.c o  m*/

    if (model == null) {
        maybeSaveLayout();
        layout = null;
        vv.setGraphLayout(null);
        return;
    }

    if (!(model instanceof GraphParrotModel)) {
        throw new IllegalArgumentException("model must be a graph model");
    }

    this.model = model;
    graph = ((GraphParrotModel) model).asGraph();
    popup = new NodeWrapperPopupMenu(SwingUtilities.getRoot(this), model);

    model.addParrotModelListener(new ParrotModelListener() {
        @Override
        public void highlightsChanged() {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    vv.fireStateChanged();
                    vv.repaint();
                }
            });
        }

        @Override
        public void restrictionsChanged(final Collection<NodeWrapper> currentlyHidden) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    includePredicate.setCurrentlyHidden(currentlyHidden);
                    vv.repaint();
                }
            });
        }

        @Override
        public void modelBusy() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    synchronized (GraphViewComponent.this.model) {
                        if (!GraphViewComponent.this.model.isBusy()) {
                            return;
                        }
                        vv.setEnabled(false);
                        view.setEnabled(false);
                        GraphViewComponent.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        view.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                        vv.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                    }
                }
            });
        }

        @Override
        public void modelIdle() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    synchronized (GraphViewComponent.this.model) {
                        if (GraphViewComponent.this.model.isBusy()) {
                            return;
                        }
                        view.setEnabled(true);
                        vv.setEnabled(true);
                        GraphViewComponent.this.setCursor(Cursor.getDefaultCursor());
                        view.setCursor(Cursor.getDefaultCursor());
                        vv.setCursor(Cursor.getDefaultCursor());
                    }
                }
            });
        }
    });

    layout = new NodeWrapperPersistentLayoutImpl(new KKLayout<NodeWrapper, NodeWrapper>(graph));
    layout.setSize(new Dimension(880, 600));
    String layoutFilename = getLayoutFilename();
    try {
        if (new File(layoutFilename).canRead()) {
            ((PersistentLayout<NodeWrapper, NodeWrapper>) layout).restore(layoutFilename);
        }
    } catch (IOException e) {
        // TODO #1 Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO #1 Auto-generated catch block
        e.printStackTrace();
    }

    vv.setGraphLayout(layout);
    GraphZoomScrollPane pane = new GraphZoomScrollPane(vv);
    ModeToggle modeToggle = new ModeToggle(mouse);
    pane.setCorner(modeToggle);
    add(pane, CONTENT_CONSTRAINTS);
    view = pane;
}