From c985ea3cc5a9b8c809bfe6b5b66357c15d3328d2 Mon Sep 17 00:00:00 2001 From: Andrew Auclair Date: Mon, 18 Dec 2023 23:23:02 -0500 Subject: [PATCH] Disable the focus on the JTabbedPanes used for docking and add support for alt + left arrow and alt + right arrow to move between tabs. (#160) --- .../internal/CustomTabbedPane.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docking-api/src/ModernDocking/internal/CustomTabbedPane.java b/docking-api/src/ModernDocking/internal/CustomTabbedPane.java index f8d3ff67..e9113ebd 100644 --- a/docking-api/src/ModernDocking/internal/CustomTabbedPane.java +++ b/docking-api/src/ModernDocking/internal/CustomTabbedPane.java @@ -23,8 +23,55 @@ of this software and associated documentation files (the "Software"), to deal import javax.swing.*; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; public class CustomTabbedPane extends JTabbedPane { + public CustomTabbedPane() { + setFocusable(false); + + getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( + KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK), + "press-left" + ); + + getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( + KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK), + "press-right" + ); + + getActionMap().put("press-left", + new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + int newIndex = getSelectedIndex() - 1; + + if (newIndex < 0) { + newIndex = getTabCount() - 1; + } + + setSelectedIndex(newIndex); + } + } + ); + + getActionMap().put("press-right", + new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + int newIndex = getSelectedIndex() + 1; + + if (newIndex >= getTabCount()) { + newIndex = 0; + } + + setSelectedIndex(newIndex); + } + } + ); + } + public int getTargetTabIndex(Point mousePosOnScreen, boolean ignoreY) { SwingUtilities.convertPointFromScreen(mousePosOnScreen, this);