How to ensure that JDialog always stays on top

I have a JDialog that accepts a username. Behind JDialog is the applet. I do not want the user to access this applet until he enters the name. I tried JDialog.setAlwaysOnTop(true) , but the applet throws an AccessException error. So what I did was leave a while loop that will execute JDialog.setVisible(true) until the JtextField (input for username) is empty (""). But for some reason it works very slowly, which means the JDialog load, but it takes time to focus on the JtextField , and even when the user enters his name, he comes very slowly ... like one character in 2 seconds ... There is is there any other way to get the user to enter a name before accessing the applet?

+6
java visibility jdialog
source share
3 answers

Use modal JDialog. For example, the code in your init (...) JApplet method may include:

 JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this)); dialog.setModal(true); dialog.setSize(...); dialog.setVisible( true ); 

Or you can just use JOptionPane.showInputDialog (). Again, you simply specify "this" as the parent component of the options bar.

+10
source share

Another variant:

 frame.setAlwaysOnTop(true); 

It forces dialogue on top of any other.

+1
source share

It runs slowly because the program processes this foo loop

What you can do is add a window listener and then jdialog will lose focus (or the applet gets it) will return focus to jdialog.

This should work much better than the for loop you are using right now

0
source share

All Articles