简体   繁体   中英

how to ensure that JDialog always stays on top

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?

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.

Another option would be:

frame.setAlwaysOnTop(true);

It forces the dialog on top of any other.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM