fixed focus in password dialog

This commit is contained in:
Radek Davidek 2026-03-27 19:33:12 +01:00
parent b193ff649a
commit be40fe132f

View File

@ -1673,20 +1673,41 @@ public class FilePanelTab extends JPanel {
public String requestPassword(String archiveName) {
final String[] result = new String[1];
try {
if (SwingUtilities.isEventDispatchThread()) {
JPasswordField pf = new JPasswordField();
int ok = JOptionPane.showConfirmDialog(FilePanelTab.this, pf, "Enter password for " + archiveName, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (ok == JOptionPane.OK_OPTION) {
result[0] = new String(pf.getPassword());
}
} else {
SwingUtilities.invokeAndWait(() -> {
JPasswordField pf = new JPasswordField();
int ok = JOptionPane.showConfirmDialog(FilePanelTab.this, pf, "Enter password for " + archiveName, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (ok == JOptionPane.OK_OPTION) {
result[0] = new String(pf.getPassword());
Runnable showPasswordDialog = () -> {
JPasswordField pf = new JPasswordField() {
@Override
public void addNotify() {
super.addNotify();
requestFocusInWindow();
}
};
Object[] message = {
"Enter password for " + archiveName,
pf
};
JOptionPane pane = new JOptionPane(message, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = pane.createDialog(FilePanelTab.this, "Password Required");
dialog.addWindowFocusListener(new java.awt.event.WindowAdapter() {
@Override
public void windowGainedFocus(java.awt.event.WindowEvent e) {
pf.requestFocusInWindow();
}
});
dialog.setVisible(true);
Object selectedValue = pane.getValue();
if (selectedValue != null && (Integer) selectedValue == JOptionPane.OK_OPTION) {
result[0] = new String(pf.getPassword());
}
};
if (SwingUtilities.isEventDispatchThread()) {
showPasswordDialog.run();
} else {
SwingUtilities.invokeAndWait(showPasswordDialog);
}
} catch (Exception e) {
result[0] = null;