Java Code Examples for java.awt.event.MouseEvent#getComponent()

The following examples show how to use java.awt.event.MouseEvent#getComponent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Handle a mouse press event - if the user is NOT resizing a column and NOT dragging a column
 * then give visual feedback that the column header has been pressed.
 *
 * @param e  the mouse event.
 */
public void mousePressed(final MouseEvent e) {

    final JTableHeader header = (JTableHeader) e.getComponent();

    if (header.getResizingColumn() == null) {  // resizing takes precedence over sorting
        if (header.getDraggedDistance() < 1) {   // dragging also takes precedence over sorting
            final int columnIndex = header.columnAtPoint(e.getPoint());
            final int modelColumnIndex 
                = header.getTable().convertColumnIndexToModel(columnIndex);
            if (this.model.isSortable(modelColumnIndex)) {
                this.sortColumnIndex = header.getTable().convertColumnIndexToModel(columnIndex);
                this.renderer.setPressedColumn(this.sortColumnIndex);
                header.repaint();
                if (header.getTable().isEditing()) {
                    header.getTable().getCellEditor().stopCellEditing();
                }
            }
            else {
                this.sortColumnIndex = -1;
            }
        }
    }

}
 
Example 2
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 6 votes vote down vote up
@Override public void mouseDragged(MouseEvent e) {
  JList<?> l = (JList<?>) e.getComponent();
  if (l.getDragEnabled()) {
    return;
  }
  Point destPoint = e.getPoint();
  Path2D rb = getRubberBand();
  rb.reset();
  rb.moveTo(srcPoint.x, srcPoint.y);
  rb.lineTo(destPoint.x, srcPoint.y);
  rb.lineTo(destPoint.x, destPoint.y);
  rb.lineTo(srcPoint.x, destPoint.y);
  rb.closePath();

  // JDK 1.7.0: l.setSelectedIndices(getIntersectsIcons(l, rubberBand));
  int[] indices = IntStream.range(0, l.getModel().getSize())
      .filter(i -> rb.intersects(l.getCellBounds(i, i))).toArray();
  l.setSelectedIndices(indices);
  l.repaint();
}
 
Example 3
Source Project: pumpernickel   File: SplayedLayout.java    License: MIT License 6 votes vote down vote up
private void process(MouseEvent e) {
	Component c = e.getComponent();
	JComponent splayedAncestor = (JComponent) getSplayedAncestor(c);
	if (splayedAncestor == null) {
		c.removeMouseListener(this);
	} else if (prioritizeRollover) {
		if (e.getID() == MouseEvent.MOUSE_ENTERED
				&& splayedAncestor
						.getClientProperty(PROPERTY_ROLLOVER_CHILD) != c) {
			splayedAncestor.putClientProperty(PROPERTY_ROLLOVER_CHILD,
					c);
			splayedAncestor.revalidate();
		} else if (e.getID() == MouseEvent.MOUSE_EXITED
				&& splayedAncestor
						.getClientProperty(PROPERTY_ROLLOVER_CHILD) == c) {
			splayedAncestor.putClientProperty(PROPERTY_ROLLOVER_CHILD,
					null);
			splayedAncestor.revalidate();
		}
	}
}
 
Example 4
@Override
public void mouseDragged(MouseEvent e) {
  if (gestureHandlers == null || gestureHandlers.isEmpty() || !listensFor(Event.DRAGGED))
    return;

  if (e.getComponent() instanceof ChartPanel) {
    ChartPanel chartPanel = (ChartPanel) e.getComponent();
    // keep the same chartEntity
    ChartEntity entity = lastDragEvent.getEntity();
    ChartGesture.Entity gestureEntity = lastDragEvent.getGesture().getEntity();
    Button button = lastDragEvent.getGesture().getButton();

    // handle event
    lastDragEvent = new ChartGestureEvent(chartPanel, e, entity,
        new ChartGesture(gestureEntity, Event.DRAGGED, button));
    handleEvent(lastDragEvent);
  }
}
 
Example 5
@Override
public void mousePressed(MouseEvent e) {
  if (gestureHandlers == null || gestureHandlers.isEmpty() || !listensFor(Event.PRESSED))
    return;

  if (e.getComponent() instanceof ChartPanel) {
    ChartPanel chartPanel = (ChartPanel) e.getComponent();
    ChartEntity entity = findChartEntity(chartPanel, e);
    ChartGesture.Entity gestureEntity = ChartGesture.getGestureEntity(entity);
    Button button = Button.getButton(e.getButton());
    // handle event
    lastDragEvent = new ChartGestureEvent(chartPanel, e, entity,
        new ChartGesture(gestureEntity, Event.PRESSED, button));
    handleEvent(lastDragEvent);
  }
}
 
Example 6
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 5 votes vote down vote up
@Override public void mouseReleased(MouseEvent e) {
  if (!window.isVisible() || Objects.isNull(draggingComponent)) {
    return;
  }
  Point pt = e.getPoint();
  Container parent = (Container) e.getComponent();

  Component cmp = draggingComponent;
  draggingComponent = null;
  window.setVisible(false);

  for (int i = 0; i < parent.getComponentCount(); i++) {
    Component c = parent.getComponent(i);
    Rectangle r = c.getBounds();
    int wd2 = r.width / 2;
    PREV_AREA.setBounds(r.x, r.y, wd2, r.height);
    NEXT_AREA.setBounds(r.x + wd2, r.y, wd2, r.height);
    if (PREV_AREA.contains(pt)) {
      swapComponentLocation(parent, gap, cmp, i > 1 ? i : 0);
      return;
    } else if (NEXT_AREA.contains(pt)) {
      swapComponentLocation(parent, gap, cmp, i);
      return;
    }
  }
  if (parent.getBounds().contains(pt)) {
    swapComponentLocation(parent, gap, cmp, parent.getComponentCount());
  } else {
    swapComponentLocation(parent, gap, cmp, index);
  }
}
 
Example 7
public void mouseEntered(MouseEvent e) {
    Component component = e.getComponent();
    if (component instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) component;
        button.setBorderPainted(false);
    }
}
 
Example 8
Source Project: java-swing-tips   File: ReorderbleList.java    License: MIT License 5 votes vote down vote up
@Override public void mouseReleased(MouseEvent e) {
  JList<?> l = (JList<?>) e.getComponent();
  l.setFocusable(true);
  // if (Objects.isNull(srcPoint) || !getDragEnabled()) {
  //   Component glassPane = l.getRootPane().getGlassPane();
  //   glassPane.setVisible(false);
  // }
  getRubberBand().reset();
  l.setDragEnabled(l.getSelectedIndices().length > 0);
  l.repaint();
}
 
Example 9
Source Project: pumpernickel   File: CurvedPolylineCreationUI.java    License: MIT License 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent evt) {
	ShapeCreationPanel scp = (ShapeCreationPanel) evt.getComponent();
	try {
		AffineTransform tx = scp.getTransform().createInverse();
		Point2D mouseLoc = new Point2D.Float(evt.getX(), evt.getY());
		Point2D abstractLoc = tx.transform(mouseLoc, null);

		boolean isCreating = ShapeCreationPanel.MODE_CREATE.equals(scp
				.getMode());
		if (!isCreating) {
			Selection selection = scp.getSelectionModel()
					.getSelection();
			if (selection.getShapeIndex() != -1) {
				if (selection.getNodeIndex() != -1) {
					CurvedPolyline shape = getMirror(scp)[selection
							.getShapeIndex()];
					shape.setPoint(selection.getNodeIndex(),
							abstractLoc.getX(), abstractLoc.getY());
					scp.getDataModel().setShape(
							selection.getShapeIndex(), shape);
				} else {
					float dx = evt.getX() - lastUntransformedX;
					float dy = evt.getY() - lastUntransformedY;
					nudge(scp, dx, dy);
				}
				return;
			}
		}
		mouseMoved(evt);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		lastUntransformedX = evt.getX();
		lastUntransformedY = evt.getY();
	}
}
 
Example 10
@Override
public void mouseEntered (MouseEvent e)
{
    Component component = e.getComponent();

    if (component instanceof AbstractButton) {
        AbstractButton button = (AbstractButton) component;
        button.setBorderPainted(true);
    }
}
 
Example 11
/**
 *  Setup the variables used to control the moving of the component:
 *
 *  source - the source component of the mouse event
 *  destination - the component that will ultimately be moved
 *  pressed - the Point where the mouse was pressed in the destination
 *      component coordinates.
 */
@Override
public void mousePressed(MouseEvent e)
{
	isMousePressed = true;
	source = e.getComponent();
	int width  = source.getSize().width  - dragInsets.left - dragInsets.right;
	int height = source.getSize().height - dragInsets.top - dragInsets.bottom;
	Rectangle r = new Rectangle(dragInsets.left, dragInsets.top, width, height);

	if (r.contains(e.getPoint()))
		setupForDragging(e);
}
 
Example 12
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 5 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  Component c = e.getComponent();
  c.setCursor(hndCursor);
  Container p = SwingUtilities.getUnwrappedParent(c);
  if (p instanceof JViewport) {
    JViewport vport = (JViewport) p;
    Point cp = SwingUtilities.convertPoint(c, e.getPoint(), vport);
    pp.setLocation(cp);
  }
}
 
Example 13
Source Project: netbeans   File: InnerPanelSupport.java    License: Apache License 2.0 5 votes vote down vote up
public void redispatchMouseEvent(MouseEvent e) {
    if (!(e.getComponent() instanceof RenderedImage)) {
        return;
    }
    MouseEvent delegate = SwingUtilities.convertMouseEvent(e.getComponent(), e, listClasses);
    listClasses.dispatchEvent(delegate);
    // if the table started editing, remove the popup:
    if (listClasses.isEditing()) {
        hidePopup();
    }
}
 
Example 14
Source Project: opt4j   File: DefaultSelectedPanel.java    License: MIT License 5 votes vote down vote up
@Override
public void mouseExited(MouseEvent e) {
	Component component = e.getComponent();
	if (component instanceof AbstractButton) {
		AbstractButton button = (AbstractButton) component;
		button.setBorderPainted(false);
	}
}
 
Example 15
Source Project: consulo   File: EditorActionUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static void showEditorPopup(final EditorMouseEvent event, @Nonnull final ActionGroup group) {
  if (!event.isConsumed() && event.getArea() == EditorMouseEventArea.EDITING_AREA) {
    ActionPopupMenu popupMenu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.EDITOR_POPUP, group);
    MouseEvent e = event.getMouseEvent();
    final Component c = e.getComponent();
    if (c != null && c.isShowing()) {
      popupMenu.getComponent().show(c, e.getX(), e.getY());
    }
    e.consume();
  }
}
 
Example 16
private void checkForPopup(MouseEvent e)
{
	if (e.isPopupTrigger())
	{
		JTableHeader header = (JTableHeader)e.getComponent();
		int column = header.columnAtPoint( e.getPoint() );
		showPopup(column);
	}
}
 
Example 17
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 5 votes vote down vote up
protected void updateToolTip(MouseEvent e) {
  JSlider slider = (JSlider) e.getComponent();
  int intValue = slider.getValue();
  if (prevValue != intValue) {
    label.setText(String.format("%03d", slider.getValue()));
    Point pt = e.getPoint();
    pt.y = -size.height;
    SwingUtilities.convertPointToScreen(pt, e.getComponent());
    pt.translate(-size.width / 2, 0);
    toolTip.setLocation(pt);
  }
  prevValue = intValue;
}
 
Example 18
Source Project: java-swing-tips   File: MainPanel.java    License: MIT License 4 votes vote down vote up
@Override public void mousePressed(MouseEvent e) {
  JComponent panel = (JComponent) e.getComponent();
  origin.setLocation(e.getPoint());
  // 選択された部品を上へ
  parent.moveToFront(panel);
}
 
Example 19
Source Project: darklaf   File: MouseEventUI.java    License: MIT License 4 votes vote down vote up
private void dispatchMouseEvent(final MouseEvent mouseEvent) {
    if (mouseEvent != null) {
        Component target = mouseEvent.getComponent();
        target.dispatchEvent(mouseEvent);
    }
}
 
Example 20
Source Project: consulo   File: RelativePoint.java    License: Apache License 2.0 4 votes vote down vote up
public RelativePoint(@Nonnull MouseEvent event) {
  this(event.getComponent(), event.getPoint());
}