Link to home
Start Free TrialLog in
Avatar of benchpresser

asked on

java swing focus problem, focus lost

i have a huge java client application in swing. it has many screens containing many textfields, buttons, tables(contains editable cells), modal dialogs, windows, checkboxes, radiobuttons etc. many of them have actions such as lose focus, gain focus attached to them. so if the user clicks, enters, changes, leaves the text fields, many actions are triggered, some open new windows, some show some messagaes, some manipulate the focused component etc.

my problem is, some of our clients gets a problem when using the pages,customer is annoying about that, on some cases, users can not enter or edit textfields anymore. they only can click buttons but it is impossible to enter textfields or type sth.

we also could generate the case, if you do some actions very fast, the problems occurs. but i have no time to deeply analyze and reorganize the code. it is huge and complex. i have to found some temporarily solution in one day immediately.

what i have done so far is listening the KeyboardFocusManager and i could catch the situation when the problem occurs. on a seperate thread, i check the permanentfocusowner each 2 seconds interval in a while loop by

Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();

Open in new window

if the type of c is one of textfields, buttons, tables, editable table cells, there is no problem. but if the type is a JPanel, (i check by instanceof) i discover that we entered into the problematic case.

To solve the issue, i done many things such as requesting focus, requestfocusinwinow, force focus to my editable components, repaint dialogs, revalidate, clearing global focus owner, .. etc. but none of them rescued the issue. we could click buttons but we can not enter into textfields or change or edit.

what solves the case is simply showing one message by
JOptionPane.showMessageDialog("null", "We entered problematic case");

Open in new window

if we click ok to the message and the message dialog disappears, our screen starts working clearly without problems, we can focus or edit to textfields, change values etc.

But of course we can not show any dummy message boxes.

 As i checked the java side, i see that the message dialogs do some priviledged actions
such as showLWModal or stopLWModal, i suppose whether they solve the issue.

Do yu have any idea, i need some trick to rescue the screen but wihtout showing any dummy messages? I think we get problems in modality when the problem occurs and showing the message solves the problem because it handles with modality and manipulates is by setting and resetting.
Avatar of HonorGod
Flag of United States of America image

wow.  Tricky situation.

Are you dynamically modifying the frame or pane by adding & removing components?
I've seen a situation where I was doing this on a SwingWorker thread, and the only way to ensure that all of the changes that were actually recognized was by adding a call to frame.validate()

Instead of calling a dummy dialog, could you create it invisibly (i.e., don't call setVisible( true ) ) and destroy it?
Avatar of benchpresser


i added a simple visible jdialog to my parent frame
i tried one with modality true and one with modailty false
when i close the opened jdialog, my problem is NOT solved.

but if i show a Joptionpane.showmessagedialog..

it gets resolved after closing the dialog.

so there is a difference between jdialog and joptionpane.sowmessagedialog..

if looked deeply, joptionpane itself creates a dialog i will check what happens here differently and solves the issue
i created a JOptionPane:

JOptionPane pane = new JOptionPane("problem occured");
JDialog jdialog = pane.createDialog(iManager.getActiveWindow(), "test");


when i create it visible, and when i click after the problem occurs,
the problem gets solved.

but when i create it hidden it does not get solved.

a very bad and tricky solution seems to be solving my problem:

a dialog closer thread which closes the dialog sized in dimension 1,1  after 10ms. :)

but i expect some less tricky solutions if possible.
Avatar of HonorGod
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nope. validate does not help.

i got also getting problems in capturing the problematic case.
because i got sometimes such a position,
where the cursor is on a textfield blinking but i con change it or go to antoher text field.
what can i do is clicking buttons.
showing the message option solves the case but i can not recognize the case
because permanentfocusowner is not jpanel this time, so my problem fetching case
(control of permanentfocusowner with instancof Jpanel) does not work.
I can relate to the frustration that this is causing you.

Unfortunately, it is exceedingly difficult to diagnose without being able to access and understand the specific details about the application.

Can you make the panel visible, but with the location almost to the MAXINT of X & Y?
i am okay with panel and visibility, i close it after 10ms and it works. but i have another problem, as i said before, catching the state i mean to determine when the problem occured, or as stated clearly, checking whether we are in trouble.

on some scenario the focus gets totally lost, and i can discover it as said above with checking instanceof Jpanel through getPermanentFocusOwner.

but on some other scenario, which i can not detect, the textfield has focus, it blinks, but i can not edit or change of focus to anywhere else. i can also solve that using temporarily message dialog, but i can not determine that we are currently in trouble.
i change my question and my problem:

i have a swing application with textfields, buttons, tables, checkboxes, radiobuttons.

on some mystery cases, the focus remains stuck on of them above. for example we are on a textfield, cursor is bliking, but we can not leave the textfield, nor change its value. clicking on other textfield does not change the focus owner. clicking tab button does not work. what we can do is clicking buttons or checkboxes or radiobuttons. but all textfields seem to be disabled, despite of the fact that they are all enabeld and editable.

my question is:

how i can detect such a situation? how can i discover that such a problem occured within java sources?
So, your textfield has the focus.  Does it have one or more InputVerifiers associated with it?

Do you have any InputVerifiers defined?
no i did not define any input verifier.

but i most check whether it has some default (defined by swing is it blank)
Well, for text fields, there isn't a default InputVerified.

Are you using a simple JTextField, or a JFormattedTextField?
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it solved the issue
Thanks for the assist, and the points.

Good luck & have a great day.