button switch with arrows

This commit is contained in:
rdavidek 2026-01-18 11:38:05 +01:00
parent bf735d99f7
commit 5454ac5a5c

View File

@ -3,6 +3,9 @@ package cz.kamma.kfmanager;
import cz.kamma.kfmanager.ui.MainWindow; import cz.kamma.kfmanager.ui.MainWindow;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
/** /**
* Main application class for KF File Manager * Main application class for KF File Manager
@ -21,6 +24,9 @@ public class MainApp {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
// Enable arrow key navigation in JOptionPane dialogs
setupGlobalKeyNavigation();
// Start GUI in Event Dispatch Thread // Start GUI in Event Dispatch Thread
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
@ -28,4 +34,39 @@ public class MainApp {
mainWindow.setVisible(true); mainWindow.setVisible(true);
}); });
} }
private static void setupGlobalKeyNavigation() {
Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
if (ke.getID() == KeyEvent.KEY_PRESSED) {
int code = ke.getKeyCode();
if (code == KeyEvent.VK_LEFT || code == KeyEvent.VK_RIGHT) {
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (focusOwner instanceof JButton) {
if (isInsideJOptionPane(focusOwner)) {
if (code == KeyEvent.VK_LEFT) {
focusOwner.transferFocusBackward();
} else {
focusOwner.transferFocus();
}
ke.consume();
}
}
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
}
private static boolean isInsideJOptionPane(Component c) {
Component parent = c;
while (parent != null) {
if (parent instanceof JOptionPane) {
return true;
}
parent = parent.getParent();
}
return false;
}
} }