javaswingmousejtabbedpanenimbus

JTabbedPane - How to scroll (not select) tabs with mouse wheel (SCROLL_TAB_LAYOUT)


When space is limited, the tabs get shown in a scroll pane, and two small arrow buttons appear. I would like to use the mouse wheel to operate these buttons.

There is a nice solution in Use mouse to scroll through tabs in JTabbedPane, but it actually selects the tabs. I only want to scroll them, without changing the selection.

How do I get these bottons or related actions?

(I am using the Nimbus LaF, if that is important.)


Solution

  • just found it! The drag-and-drop code in DnDTabbedPane at https://java-swing-tips.blogspot.co.at/2008/04/drag-and-drop-tabs-in-jtabbedpane.html has this very nice method:

    private void clickArrowButton(String actionKey) {
        ActionMap map = getActionMap();
        if (map != null) {
            Action action = map.get(actionKey);
            if (action != null && action.isEnabled()) {
                action.actionPerformed(new ActionEvent(
                        this, ActionEvent.ACTION_PERFORMED, null, 0, 0));
            }
        }
    }
    

    and so I just need to say:

        addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (e.getWheelRotation() > 0) {
                    clickArrowButton("scrollTabsBackwardAction");
                } else {
                    clickArrowButton("scrollTabsForwardAction");
                }
            }
        });