From a505534197b0b549996956ccfffe44d406344612 Mon Sep 17 00:00:00 2001 From: Jan S Date: Tue, 26 Apr 2022 18:27:39 +0200 Subject: [PATCH] fix(gui): fix IndexOutOfBoundsException when switching between tabs via mouse wheel (#1456)(PR #1469) --- .../src/main/java/jadx/gui/ui/TabbedPane.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java b/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java index 2ca0fc6d4..65ad47836 100644 --- a/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java +++ b/jadx-gui/src/main/java/jadx/gui/ui/TabbedPane.java @@ -54,20 +54,26 @@ public class TabbedPane extends JTabbedPane { setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); - addMouseWheelListener(e -> { - if (openTabs.isEmpty()) { + addMouseWheelListener(event -> { + int direction = event.getWheelRotation(); + if (openTabs.isEmpty() || direction == 0) { return; } - int direction = e.getWheelRotation(); + direction = (direction < 0) ? -1 : 1; // normalize direction int index = getSelectedIndex(); int maxIndex = getTabCount() - 1; - if ((index == 0 && direction < 0) - || (index == maxIndex && direction > 0)) { - index = maxIndex - index; - } else { - index += direction; + index += direction; + // switch between first tab <-> last tab + if (index < 0) { + index = maxIndex; + } else if (index > maxIndex) { + index = 0; + } + try { + setSelectedIndex(index); + } catch (IndexOutOfBoundsException e) { + // ignore error } - setSelectedIndex(index); }); interceptTabKey(); enableSwitchingTabs();