I apologise ahead of time of lack of reproducable example, the app i have is very big, and the problem seems to be related to weird combination of controls and focus switches, which i haven't been able to reproduce with a shorter program.
Basically, i have an text editor application (in java8 on windows 7) with a JFrame and a JTabbedPane with each tab containing a JSplitPane of text area and another tab displaying some information. I have implemented a tab switcher system like in Eclipse, Ctrl-Tab shows a list of editors in a modal JDialog. This is implemented as a KeyListener which keeps track of various keys pressed. The code looks something like this:
private TabsDialog td = new TabsDialog(mainFrame, true);
void onKeyPressCtrlTab()
{
td.setVisible(true); //display tab dialog
//After closing dialog, select correct tab
TabContent tc = td.getSelectedTab();
switchToTab(tc);
}
void onKeyRelease()
{
dispatchDialog();
}
void dispatchDialog()
{
if (td.isVisible())
{
td.setVisible(false);
}
}
The problem is, if i invoke onKeyPressCtrlTab and onKeyRelease in quick succession by pressing and releasing Ctrl-Tab, the focus system of the main application stops working, i can click on the main window and select text with the mouse, but the caret in text components isn't being shown at all. I can't type any text into any of the text components either. Also, all requestFocusInWindow calls are failing.
The problem is reproducable every time, but sometimes it takes 5 of Ctrl-Tabs, sometimes longer.
I've traced the problem to Component#requestFocusHelper, the following call always fail after the problem appears:
final boolean requestFocusHelper (boolean temporary,
boolean focusedWindowChangeAllowed,
CausedFocusEvent.Cause cause)
...
boolean success = peer.requestFocus
(this, temporary, focusedWindowChangeAllowed, time, cause); //fails
It seems that calling a new modal dialog (for example JOptionPane#showConfirmDialog) restores the focus system, but this is not a great solution.
I have spend two days on this issue, but haven't been able to find any good solution. It hasn't helped to dispose and re-create the TabsDialog on every call. I suspect there's something wrong with how the dialogs are cleaned up / enabled in Windows.
I think i'm doing all work on AWT thread so i doubt that's the issue. There are "some" things going on when switching tabs, i have a splitpane which is set / restored since each tab remembers the position, but still, nothing that should affect the focus system in my opinion. I have a uncatched exception in thread filter, so no exceptions are supressed as far as i can see.
Appreciate any thoughts on the problem, and a potential solution (even workaround that restores the focus would be great)
Here's a demonstration of how it's supposed to work:
https://gist.github.com/siggemannen/4affdff4b1892a15e481c626a190efab