开发者

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 r开发者_如何学编程eally 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜