List of usage examples for javax.swing SwingUtilities getDeepestComponentAt
public static Component getDeepestComponentAt(Component parent, int x, int y)
parent
that contains the location x
, y
. From source file:Main.java
public static void main(String[] args) { JComboBox combo = new JComboBox(); combo.setEditable(true);/*from www . j ava 2 s . com*/ for (int i = 0; i < 10; i++) { combo.addItem(i); } JLabel tip = new JLabel(); tip.setText("Outside combobox"); JPanel panel = new JPanel(); panel.add(combo); panel.add(tip); panel.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { tip.setText("Outside combobox"); } @Override public void mouseExited(MouseEvent e) { Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); tip.setText(c != null && SwingUtilities.isDescendingFrom(c, combo) ? "Inside combo box" : "Outside combobox"); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); }
From source file:Main.java
/** * <p>/*from www .j a v a2 s. co m*/ * Determines if the component is visible in its window at the given screen location. * </p> * * @param location A location on the screen. * @param component A component in a window. * @return True if the component is visible in its window at the given screen location. */ public static boolean locationInComponentVisible(Point location, Component component) { // Get the root component in the window. JRootPane rootPane = getRootPane(component); if (rootPane != null) { Component rootComponent = rootPane.getContentPane(); if (rootComponent != null) { // Get the location relative to this root component. Point locationInRoot = new Point(location); SwingUtilities.convertPointFromScreen(locationInRoot, rootComponent); // Get the deepest visible component at the given location. Component deepestComponent = SwingUtilities.getDeepestComponentAt(rootComponent, locationInRoot.x, locationInRoot.y); if (deepestComponent != null) { boolean result = SwingUtilities.isDescendingFrom(deepestComponent, component); return result; } } } return false; }
From source file:it.unibas.spicygui.vista.listener.MyMouseEventListener.java
public void mousePressed(MouseEvent e) { e.consume();//from w w w.jav a 2s .c o m Component comS = SwingUtilities.getDeepestComponentAt(pannelloPrincipale, e.getX(), e.getY()); if ((comS instanceof JScrollBar || !((comS instanceof JPanel) || (comS instanceof JTree))) && comS != null) { if (tmp12 == null) { tmp12 = comS; } } if (!(Costanti.INTERMEDIE.equals(comS.getName()))) { dispacciaEvento(null, e, e.getPoint(), false); } jLayeredPane.moveToFront(component); component.updateUI(); }
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; }/*ww w.j a v a2 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); }
From source file:it.unibas.spicygui.vista.listener.MyMouseEventListener.java
private void dispacciaEvento(Component com, MouseEvent e, Point point, boolean draggedEvent) { if (com == null) { com = SwingUtilities.getDeepestComponentAt(pannelloPrincipale, point.x, point.y); }/*from w w w .j a v a2 s .c o m*/ Point componentPoint = SwingUtilities.convertPoint(component, e.getPoint(), com); if (com != null) { if (com instanceof JTree) { TreePath treePath = ((JTree) com).getPathForLocation(componentPoint.x, componentPoint.y); if (treePath == null || draggedEvent) { jLayeredPane.moveToFront(component); component.updateUI(); if (e.getButton() == MouseEvent.BUTTON3) { return; } } } com.dispatchEvent(new MouseEvent(com, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); jLayeredPane.moveToFront(component); component.updateUI(); } jLayeredPane.moveToFront(component); component.updateUI(); }
From source file:components.GlassPaneDemo.java
private void redispatchMouseEvent(MouseEvent e, boolean repaint) { Point glassPanePoint = e.getPoint(); Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane); if (containerPoint.y < 0) { //we're not in the content pane if (containerPoint.y + menuBar.getHeight() >= 0) { //The mouse event is over the menu bar. //Could handle specially. } else {//from w ww . ja v a 2 s . c o m //The mouse event is over non-system window //decorations, such as the ones provided by //the Java look and feel. //Could handle specially. } } else { //The mouse event is probably over the content pane. //Find out exactly which component it's over. Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y); if ((component != null) && (component.equals(liveButton))) { //Forward events over the check box. Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } } //Update the glass pane if requested. if (repaint) { glassPane.setPoint(glassPanePoint); glassPane.repaint(); } }
From source file:GlassPaneDemo.java
private void redispatchMouseEvent(MouseEvent e, boolean repaint) { Point glassPanePoint = e.getPoint(); Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane); if (containerPoint.y < 0) { // we're not in the content pane if (containerPoint.y + menuBar.getHeight() >= 0) { // The mouse event is over the menu bar. // Could handle specially. } else {/*from w ww . j a v a 2 s .co m*/ // The mouse event is over non-system window // decorations, such as the ones provided by // the Java look and feel. // Could handle specially. } } else { // The mouse event is probably over the content pane. // Find out exactly which component it's over. Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y); if ((component != null) && (component.equals(liveButton))) { // Forward events over the check box. Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } } // Update the glass pane if requested. if (repaint) { glassPane.setPoint(glassPanePoint); glassPane.repaint(); } }
From source file:com.hp.alm.ali.idea.content.taskboard.TaskBoardPanel.java
private <T extends Component> T locateContainer(MouseEvent event, Class<T> clazz) { Point p = SwingUtilities.convertPoint((Component) event.getSource(), event.getPoint(), TaskBoardPanel.this); Component comp = SwingUtilities.getDeepestComponentAt(this, p.x, p.y); return (T) SwingUtilities.getAncestorOfClass(clazz, comp); }
From source file:SwingGlassExample.java
private void redispatchMouseEvent(MouseEvent e) { boolean inButton = false; boolean inMenuBar = false; Point glassPanePoint = e.getPoint(); Component component = null;/*w ww . java 2s .c o m*/ Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, contentPane); int eventID = e.getID(); if (containerPoint.y < 0) { inMenuBar = true; container = menuBar; containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, menuBar); testForDrag(eventID); } //XXX: If the event is from a component in a popped-up menu, //XXX: then the container should probably be the menu's //XXX: JPopupMenu, and containerPoint should be adjusted //XXX: accordingly. component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y); if (component == null) { return; } else { inButton = true; testForDrag(eventID); } if (inMenuBar || inButton || inDrag) { Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, eventID, e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } }
From source file:brainflow.app.toplevel.BrainFlow.java
private void showActionMenu(MouseEvent e) { Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); java.util.List<Action> actionList = new ArrayList<Action>(); while (true) { if (c instanceof IActionProvider) { IActionProvider provider = (IActionProvider) c; provider.addActions(e, actionList); } else if (c instanceof JComponent) { JComponent jc = (JComponent) c; Object provider = jc.getClientProperty(IActionProvider.KEY); if (provider != null) { ((IActionProvider) provider).addActions(e, actionList); }// w ww .j a v a 2s . c o m } Component p = c.getParent(); if (p != null) { c = p; } else { break; } } if (actionList.size() > 0) { createPopup(actionList).setVisible(true); } }