Example usage for javax.swing SwingUtilities getAncestorOfClass

List of usage examples for javax.swing SwingUtilities getAncestorOfClass

Introduction

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

Prototype

public static Container getAncestorOfClass(Class<?> c, Component comp) 

Source Link

Document

Convenience method for searching above comp in the component hierarchy and returns the first object of class c it finds.

Usage

From source file:Main.java

public static void repaintAllParent(JComponent component) {
    JComponent parentComponent = (JComponent) SwingUtilities.getAncestorOfClass(JComponent.class, component);

    do {/*  www.  j  a v  a  2  s.  com*/
        repaintParent(parentComponent);
        parentComponent = (JComponent) SwingUtilities.getAncestorOfClass(JComponent.class, parentComponent);

    } while (parentComponent != null);

}

From source file:Main.java

/**
 * Scrolls the given component to its top or bottom.
 * Useful for implementing behavior like in Apple's Mail where home/end in the list cause scrolling in the text.
 *//* ww w  .  j  a  va  2  s  .co  m*/
public static void scrollToExtremity(JComponent c, boolean top) {
    JScrollBar scrollBar = ((JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, c))
            .getVerticalScrollBar();
    scrollBar.setValue(top ? scrollBar.getMinimum() : scrollBar.getMaximum());
}

From source file:Main.java

/**
 * Returns the JDesktopPane of the specified Component. <br />
 * /*from  w  w  w .  j  ava 2 s. co m*/
 * @param c the component
 * @return the JDesktopPane for the component
 */
public static JDesktopPane getDesktop(Component c) {
    return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class, c);
}

From source file:Main.java

/**
 * Scrolls the given component by its unit or block increment in the given direction (actually a scale factor, so use +1 or -1).
 * Useful for implementing behavior like in Apple's Mail where page up/page down in the list cause scrolling in the text.
 *//*from  w  ww  .  j  a  v  a  2  s .co  m*/
public static void scroll(JComponent c, boolean byBlock, int direction) {
    JScrollPane scrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, c);
    JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
    int increment = byBlock ? scrollBar.getBlockIncrement(direction) : scrollBar.getUnitIncrement(direction);
    int newValue = scrollBar.getValue() + direction * increment;
    newValue = Math.min(newValue, scrollBar.getMaximum());
    newValue = Math.max(newValue, scrollBar.getMinimum());
    scrollBar.setValue(newValue);
}

From source file:Main.java

/**
 * Repaints the parent of the given component. If the parent is null, the component itself is repainted.
 * /*w ww .j  av a  2s  . c o m*/
 * @param    component      The component whose parent will be repainted.
 */
public static void repaintParent(JComponent component) {

    // Get the parent of the component.
    JComponent parentComponent = (JComponent) SwingUtilities.getAncestorOfClass(JComponent.class, component);

    // Could we find a parent?
    if (parentComponent != null) {
        // Repaint the parent.
        parentComponent.revalidate();
        parentComponent.repaint();
    } else {
        // Repaint the component itself.
        component.revalidate();
        component.repaint();
    }

}

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  a v  a 2s.  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:FrozenColumnHeader.java

public void addNotify() {
    TableColumn column;/*www.j  a va2 s.co m*/
    super.addNotify();
    TableColumnModel mainModel = mainTable.getColumnModel();
    TableColumnModel headerModel = new DefaultTableColumnModel();
    int frozenWidth = 0;
    for (int i = 0; i < columnCount; i++) {
        column = mainModel.getColumn(0);
        mainModel.removeColumn(column);
        headerModel.addColumn(column);
        frozenWidth += column.getPreferredWidth() + headerModel.getColumnMargin();
    }
    headerTable.setColumnModel(headerModel);
    Component columnHeader = getColumnHeader().getView();
    getColumnHeader().setView(null);
    JScrollPane mainScrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, mainTable);
    mainScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JLabel("..."));

    headerTable.setPreferredScrollableViewportSize(new Dimension(frozenWidth, 0));
}

From source file:com.sshtools.common.ui.OptionsPanel.java

/**
 *
 *
 * @param parent/*from w ww  .  j  av a 2 s.c o  m*/
 * @param tabs tabs
 *
 * @return
 */
public static boolean showOptionsDialog(Component parent, OptionsTab[] tabs) {
    final OptionsPanel opts = new OptionsPanel(tabs);
    opts.reset();

    JDialog d = null;
    Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class, parent);

    if (w instanceof JDialog) {
        d = new JDialog((JDialog) w, "Options", true);
    } else if (w instanceof JFrame) {
        d = new JDialog((JFrame) w, "Options", true);
    } else {
        d = new JDialog((JFrame) null, "Options", true);
    }

    final JDialog dialog = d;

    //  Create the bottom button panel
    final JButton cancel = new JButton("Cancel");
    cancel.setMnemonic('c');
    cancel.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            opts.cancelled = true;
            dialog.setVisible(false);
        }
    });

    final JButton ok = new JButton("Ok");
    ok.setMnemonic('o');
    ok.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (opts.validateTabs()) {
                dialog.setVisible(false);
            }
        }
    });
    dialog.getRootPane().setDefaultButton(ok);

    JPanel buttonPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.insets = new Insets(6, 6, 0, 0);
    gbc.weighty = 1.0;
    UIUtil.jGridBagAdd(buttonPanel, ok, gbc, GridBagConstraints.RELATIVE);
    UIUtil.jGridBagAdd(buttonPanel, cancel, gbc, GridBagConstraints.REMAINDER);

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
    southPanel.add(buttonPanel);

    //
    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    mainPanel.add(opts, BorderLayout.CENTER);
    mainPanel.add(southPanel, BorderLayout.SOUTH);

    // Show the dialog
    dialog.getContentPane().setLayout(new GridLayout(1, 1));
    dialog.getContentPane().add(mainPanel);
    dialog.pack();
    dialog.setResizable(true);
    UIUtil.positionComponent(SwingConstants.CENTER, dialog);
    dialog.setVisible(true);

    if (!opts.cancelled) {
        opts.applyTabs();
    }

    return !opts.cancelled;
}

From source file:net.chaosserver.timelord.swingui.PreviousDayPanel.java

/**
 * Listens for action from this panel. If the pick date button is choosen
 * displays a Calendar allowing a user to select a date for display.
 *
 * @param evt the event triggering things
 *///  ww w .ja v  a  2s. c  om
public void actionPerformed(ActionEvent evt) {
    if (ACTION_PICK_DAY.equals(evt.getActionCommand())) {
        Frame ownerFrame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, this);
        JCalendarDialog calendarDialog = new JCalendarDialog(ownerFrame, getDisplayDate());

        calendarDialog.pack();

        Point ownerFrameLocation = ownerFrame.getLocation();
        ownerFrameLocation.setLocation(ownerFrameLocation.getX() + LayoutConstants.CHILD_FRAME_X_OFFSET,
                ownerFrameLocation.getY() + LayoutConstants.CHILD_FRAME_Y_OFFSET);

        calendarDialog.setLocation(ownerFrameLocation);
        calendarDialog.setVisible(true);

        Date choosenDate = calendarDialog.getChoosenDate();

        if (choosenDate != null) {
            setDisplayDate(choosenDate);
        }
    }
}

From source file:Main.java

@Override
public boolean isCellEditable(final EventObject event) {
    Object source = event.getSource();
    if (!(source instanceof JTree) || !(event instanceof MouseEvent)) {
        return false;
    }//from   w w w .j  a va2 s . c o m
    JTree tree = (JTree) source;
    MouseEvent mouseEvent = (MouseEvent) event;
    TreePath path = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
    if (path == null) {
        return false;
    }
    Object node = path.getLastPathComponent();
    if (node == null || !(node instanceof DefaultMutableTreeNode)) {
        return false;
    }

    Rectangle r = tree.getPathBounds(path);
    if (r == null) {
        return false;
    }
    Dimension d = panel.getPreferredSize();
    r.setSize(new Dimension(d.width, r.height));
    if (r.contains(mouseEvent.getX(), mouseEvent.getY())) {
        Point pt = SwingUtilities.convertPoint(tree, mouseEvent.getPoint(), panel);
        Object o = SwingUtilities.getDeepestComponentAt(panel, pt.x, pt.y);
        if (o instanceof JComboBox) {
            comboBox.showPopup();
        } else if (o instanceof Component) {
            Object oo = SwingUtilities.getAncestorOfClass(JComboBox.class, (Component) o);
            if (oo instanceof JComboBox) {
                comboBox.showPopup();
            }
        }
        return true;
    }
    return delegate.isCellEditable(event);
}