2

我有一个JTable里面的JPanel。我可以使用鼠标的滚轮上下JPanel滚动,但是当我的鼠标悬停在 上时JTable,我必须将其移出表格才能JPanel使用滚轮向上滚动。JPanel如果鼠标悬停在上面,有没有办法可以使用滚轮上下滚动JTable

4

2 回答 2

2

我在上面的评论中采纳了至强的建议,​​并实现了一个鼠标滚轮监听器,它将鼠标滚轮事件转发到父组件。请参阅下面的代码。

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());
  }

}
于 2012-07-09T21:46:59.860 回答
1

感谢 denshaotoko 分享您的代码。我已经按照相同的思路(事件转发)实现了一个解决方案,但将它直接放入滚动窗格中。认为它可能对其他人有用。

/**
 * 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);
        }
    }
}
于 2012-10-19T13:37:52.640 回答