I have a JDialog
that takes a name from the user. Behind the JDialog
, is an applet. I dont want the user to access that applet until he has entered the name. I tried JDialog.setAlwaysOnTop(true)
, but the applet throws an AccessException
error. So what I did was keep a while loop that will execute JDialog.setVisible(true)
till the JtextField
(input for user name) is empty (""). But for some reason this works really slow, meaning the JDialog
loads, but it takes time to focus on the JTextField
and even when the user types his name, it comes really slow... like one character in 2 seconds... Is there any other way for me to force the user to enter the name before accessing the applet?
Asked
Active
Viewed 1.7k times
7

Madhawa Priyashantha
- 9,633
- 7
- 33
- 60

mithun1538
- 1,447
- 8
- 25
- 32
3 Answers
12
Use a modal JDialog. For example the code in your init(...) method of JApplet might include:
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));
dialog.setModal(true);
dialog.setSize(...);
dialog.setVisible( true );
Or you can just use a JOptionPane.showInputDialog(). Again you would just specify "this" as the parent component of the option pane.

camickr
- 321,443
- 19
- 166
- 288
-
But theres one problem. ActionListeners are no longer responding. And also, the user can close the dialog (there is an x at the top right of the dialog). Is there anything I need to restore after the user has entered the name? – mithun1538 Apr 14 '10 at 14:16
-
If you don't want the user to be able to close the dialog then use: dialog.setDefaultCloseOperation(...); – camickr Apr 14 '10 at 15:11
3
Another option would be:
frame.setAlwaysOnTop(true);
It forces the dialog on top of any other.

Bogdan M.
- 2,161
- 6
- 31
- 53
0
It runs slowly because the program is processing that foo loop
What you can do is to add a window listener and then the jdialog lost it's focus ( or the applet gains it ) return the focus to the jdialog.
This should perform much better than the for loop you're using right now

OscarRyz
- 196,001
- 113
- 385
- 569
-
If ModalityTypes work for applets, I would suggest that instead. I don't have time to find out, though. – Michael Myers Apr 13 '10 at 18:27
-
I added a FocusListener to JDialog and wrote the body for the focusLost() function in it. However, it doesnt seem to work. If the user clicks outide the JDialog, it doesn't display again. I think clicking outside the JDialog doesn't cause the JDialog to lose focus, it just gets hidden. – mithun1538 Apr 13 '10 at 18:31