This is timing dependent, but it can happen that a modal dialog doesn't receive WINDOW_ACTIVATION & WINDOW_GAINED_FOCUS events when first shown. The following test forces "the right timings" & reproduces the bug:
public class Main {
public static void main(String[] args) throws InterruptedException {
EventQueue.invokeLater(() -> runGUI());
}
static void runGUI() {
JFrame f = new JFrame("frame");
JDialog d = new MyModalDialog(f, "dialog");
d.addWindowListener(new WindowAdapter() {
@Override
public void windowActivated(WindowEvent e) {
System.out.println(e);
}
});
d.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
System.out.println(e);
}
});
f.setVisible(true);
d.setVisible(true);
}
static class MyModalDialog extends JDialog {
public MyModalDialog(Frame owner, String title) {
super(owner, title, true);
}
@Override
public boolean getFocusableWindowState() {
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
}
return super.getFocusableWindowState();
}
}
}