下面列出了java.awt.event.MouseEvent#getComponent() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
}
}
}
@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();
}
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();
}
}
}
@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);
}
}
@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);
}
}
@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);
}
}
public void mouseEntered(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
@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();
}
@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();
}
}
@Override
public void mouseEntered (MouseEvent e)
{
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(true);
}
}
/**
* 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);
}
@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);
}
}
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();
}
}
@Override
public void mouseExited(MouseEvent e) {
Component component = e.getComponent();
if (component instanceof AbstractButton) {
AbstractButton button = (AbstractButton) component;
button.setBorderPainted(false);
}
}
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();
}
}
private void checkForPopup(MouseEvent e)
{
if (e.isPopupTrigger())
{
JTableHeader header = (JTableHeader)e.getComponent();
int column = header.columnAtPoint( e.getPoint() );
showPopup(column);
}
}
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;
}
@Override public void mousePressed(MouseEvent e) {
JComponent panel = (JComponent) e.getComponent();
origin.setLocation(e.getPoint());
// 選択された部品を上へ
parent.moveToFront(panel);
}
private void dispatchMouseEvent(final MouseEvent mouseEvent) {
if (mouseEvent != null) {
Component target = mouseEvent.getComponent();
target.dispatchEvent(mouseEvent);
}
}
public RelativePoint(@Nonnull MouseEvent event) {
this(event.getComponent(), event.getPoint());
}