views:

268

answers:

2

Hello everyone

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?

A: 

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
If ModalityTypes work for applets, I would suggest that instead. I don't have time to find out, though.
Michael Myers
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
mmhhh, can you post some sample code?
OscarRyz
+4  A: 

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
just what I was looking for. Thanks.
mithun1538
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
If you don't want the user to be able to close the dialog then use: dialog.setDefaultCloseOperation(...);
camickr