I have a JTable inside a JPanel. I can scroll up and down the JPanel using the mouse's scroll wheel, but when my mouse is hovering over the JTable, I have to move it out of the table to scroll back up the JPanel using the scroll wheel. Is there a way I can scroll up and down the JPanel using the scroll wheel if the mouse is hovering over the JTable? 
            Asked
            
        
        
            Active
            
        
            Viewed 2,592 times
        
    2
            
            
        
        mKorbel
        
- 109,525
 - 20
 - 134
 - 319
 
        BJ Dela Cruz
        
- 5,194
 - 13
 - 51
 - 84
 
- 
                    4I've just implemented the case you described but I can scroll `JPanel` event if mouse pointer is on `JTable`. Can you post SSCCE? – Xeon Jun 07 '12 at 00:31
 - 
                    2But basically you can forward wheel scroll event (`MouseWheelListener`) to your `JPanel`'s `JScrollPane` – Xeon Jun 07 '12 at 00:40
 - 
                    1or (scalable could be) to consume events came from :-), for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Jun 07 '12 at 06:47
 - 
                    Better explained [here](http://stackoverflow.com/questions/12911506/why-jscrollpane-does-not-react-to-mouse-wheel-events) (if the root problem is not caused by a JTable, but a Swing bug). Maybe a duplicate. – Juan Calero Oct 17 '13 at 16:28
 
2 Answers
2
            I took Xeon's advice in the comment above and implemented a mouse wheel listener that forwards mouse wheel events to the parent component. See the code below.
public class CustomMouseWheelListener implements MouseWheelListener {
  private JScrollBar bar;
  private int previousValue = 0;
  private JScrollPane parentScrollPane;
  private JScrollPane customScrollPane;
  /** @return The parent scroll pane, or null if there is no parent. */
  private JScrollPane getParentScrollPane() {
    if (this.parentScrollPane == null) {
      Component parent = this.customScrollPane.getParent();
      while (!(parent instanceof JScrollPane) && parent != null) {
        parent = parent.getParent();
      }
      this.parentScrollPane = (JScrollPane) parent;
    }
    return this.parentScrollPane;
  }
  /**
   * Creates a new CustomMouseWheelListener.
   * @param customScrollPane The scroll pane to which this listener belongs.
   */
  public CustomMouseWheelListener(JScrollPane customScrollPane) {
    ValidationUtils.checkNull(customScrollPane);
    this.customScrollPane = customScrollPane;
    this.bar = this.customScrollPane.getVerticalScrollBar();
  }
  /** {@inheritDoc} */
  @Override
  public void mouseWheelMoved(MouseWheelEvent event) {
    JScrollPane parent = getParentScrollPane();
    if (parent != null) {
      if (event.getWheelRotation() < 0) {
        if (this.bar.getValue() == 0 && this.previousValue == 0) {
          parent.dispatchEvent(cloneEvent(event));
        }
      }
      else {
        if (this.bar.getValue() == getMax() && this.previousValue == getMax()) {
          parent.dispatchEvent(cloneEvent(event));
        }
      }
      this.previousValue = this.bar.getValue();
    }
    else {
      this.customScrollPane.removeMouseWheelListener(this);
    }
  }
  /** @return The maximum value of the scrollbar. */
  private int getMax() {
    return this.bar.getMaximum() - this.bar.getVisibleAmount();
  }
  /**
   * Copies the given MouseWheelEvent.
   * 
   * @param event The MouseWheelEvent to copy.
   * @return A copy of the mouse wheel event.
   */
  private MouseWheelEvent cloneEvent(MouseWheelEvent event) {
    return new MouseWheelEvent(getParentScrollPane(), event.getID(), event.getWhen(),
        event.getModifiers(), 1, 1, event.getClickCount(), false, event.getScrollType(),
        event.getScrollAmount(), event.getWheelRotation());
  }
}
        BJ Dela Cruz
        
- 5,194
 - 13
 - 51
 - 84
 
1
            
            
        Thanks denshaotoko for sharing your code. I've implemented a solution along the same lines (event forwarding) but put it into the scroll pane directly. Thought it might be useful to others.
/**
 * Scroll pane that only scrolls when it owns focus. When not owning focus (i.e. mouse
 * hover), propagates mouse wheel events to its container.
 * <p>
 * This is a solution for <i>"I have a JTable inside a JPanel. When my mouse is hovering
 * over the JTable, I have to move it out of the table to scroll the JPanel."</i>
 */
public class ScrollWhenFocusedPane extends JScrollPane {
    // Note: don't leave users with "scroll on focus" behaviour
    // on widgets that they cannot focus. These will be okay.
    public ScrollWhenFocusedPane (JTree view) {super (view);}
    public ScrollWhenFocusedPane (JList view) {super (view);}
    public ScrollWhenFocusedPane (JTable view) {super (view);}
    public ScrollWhenFocusedPane (JTextArea view) {super (view);}
    @Override
    protected void processMouseWheelEvent (MouseWheelEvent evt) {
        Component outerWidget = SwingUtilities.getAncestorOfClass (Component.class, this);
        // Case 1: we don't have focus, so we don't scroll
        Component innerWidget = getViewport().getView();
        if (!innerWidget.hasFocus())
            outerWidget.dispatchEvent(evt);
        // Case 2: we have focus
        else {
            JScrollBar innerBar = getVerticalScrollBar();
            if (!innerBar.isShowing()) // Deal with horizontally scrolling widgets
                innerBar = getHorizontalScrollBar();
            boolean wheelUp = evt.getWheelRotation() < 0;
            boolean atTop = (innerBar.getValue() == 0);
            boolean atBottom = (innerBar.getValue() == (innerBar.getMaximum() - innerBar.getVisibleAmount()));
            // Case 2.1: we've already scrolled as much as we could
            if ((wheelUp & atTop) || (!wheelUp & atBottom))
                outerWidget.dispatchEvent(evt);
            // Case 2.2: we'll scroll
            else
                super.processMouseWheelEvent (evt);
        }
    }
}
        space programmer
        
- 51
 - 3