Skip to content

Commit

Permalink
Disable the focus on the JTabbedPanes used for docking and add suppor…
Browse files Browse the repository at this point in the history
…t for alt + left arrow and alt + right arrow to move between tabs. (#160)
  • Loading branch information
andrewauclair committed Jan 1, 2024
1 parent 1e9adf0 commit 9b0a0d3
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docking-api/src/ModernDocking/internal/CustomTabbedPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9b0a0d3

Please sign in to comment.