Class PanningMouseAdapter

java.lang.Object
java.awt.event.MouseAdapter
org.emrick.project.PanningMouseAdapter
All Implemented Interfaces:
MouseListener, MouseMotionListener, MouseWheelListener, EventListener

class PanningMouseAdapter extends MouseAdapter
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 Details

    • component

      private final JComponent component
    • scrollPane

      private final JScrollPane scrollPane
    • holdPoint

      private Point holdPoint
    • afterPanAction

      private Runnable afterPanAction
  • Constructor Details

    • PanningMouseAdapter

      public PanningMouseAdapter(JComponent component, JScrollPane scrollPane)
      Creates a new panning adapter for the specified component and scroll pane
      Parameters:
      component - The component to enable panning for
      scrollPane - The scroll pane containing the component
    • PanningMouseAdapter

      public PanningMouseAdapter(JComponent component, JScrollPane scrollPane, Runnable afterPanAction)
      Creates a new panning adapter with a callback that runs after each pan movement
      Parameters:
      component - The component to enable panning for
      scrollPane - The scroll pane containing the component
      afterPanAction - 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

      public void mousePressed(MouseEvent e)
      Specified by:
      mousePressed in interface MouseListener
      Overrides:
      mousePressed in class MouseAdapter
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Specified by:
      mouseReleased in interface MouseListener
      Overrides:
      mouseReleased in class MouseAdapter
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      Specified by:
      mouseDragged in interface MouseMotionListener
      Overrides:
      mouseDragged in class MouseAdapter
    • passMiddleMouseEvents

      public static MouseAdapter passMiddleMouseEvents(JComponent component, JComponent target)
      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 events
      target - The target component to which events should be forwarded
      Returns:
      The MouseAdapter that was added to the component for event forwarding