Package org.emrick.project
Class PanningMouseAdapter
java.lang.Object
java.awt.event.MouseAdapter
org.emrick.project.PanningMouseAdapter
- All Implemented Interfaces:
MouseListener,MouseMotionListener,MouseWheelListener,EventListener
Scroll-click drag panning adapter implementation
Provides smooth, precise panning of Swing components using the scroll-click (middle mouse button)
This implementation directly manipulates the viewport position to ensure content
moves exactly with the mouse cursor, providing the most natural panning experience
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Runnableprivate final JComponentprivate Pointprivate final JScrollPane -
Constructor Summary
ConstructorsConstructorDescriptionPanningMouseAdapter(JComponent component, JScrollPane scrollPane) Creates a new panning adapter for the specified component and scroll panePanningMouseAdapter(JComponent component, JScrollPane scrollPane, Runnable afterPanAction) Creates a new panning adapter with a callback that runs after each pan movement -
Method Summary
Modifier and TypeMethodDescriptionprivate voidSafely initialize the mouse listeners to avoid "leaking this" warningvoidvoidvoidstatic MouseAdapterpassMiddleMouseEvents(JComponent component, JComponent target) Makes a component transparent to middle mouse events and passes them through to a target component.Methods inherited from class java.awt.event.MouseAdapter
mouseClicked, mouseEntered, mouseExited, mouseMoved, mouseWheelMoved
-
Field Details
-
component
-
scrollPane
-
holdPoint
-
afterPanAction
-
-
Constructor Details
-
PanningMouseAdapter
Creates a new panning adapter for the specified component and scroll pane- Parameters:
component- The component to enable panning forscrollPane- The scroll pane containing the component
-
PanningMouseAdapter
Creates a new panning adapter with a callback that runs after each pan movement- Parameters:
component- The component to enable panning forscrollPane- The scroll pane containing the componentafterPanAction- Action to run after each pan movement (e.g., repainting other components)
-
-
Method Details
-
initializeListeners
private void initializeListeners()Safely initialize the mouse listeners to avoid "leaking this" warning -
mousePressed
- Specified by:
mousePressedin interfaceMouseListener- Overrides:
mousePressedin classMouseAdapter
-
mouseReleased
- Specified by:
mouseReleasedin interfaceMouseListener- Overrides:
mouseReleasedin classMouseAdapter
-
mouseDragged
- Specified by:
mouseDraggedin interfaceMouseMotionListener- Overrides:
mouseDraggedin classMouseAdapter
-
passMiddleMouseEvents
Makes a component transparent to middle mouse events and passes them through to a target component. This allows clicking through widgets like buttons and labels to enable panning on the underlying component.- Parameters:
component- The component to make transparent to middle mouse eventstarget- The target component to which events should be forwarded- Returns:
- The MouseAdapter that was added to the component for event forwarding
-