Kevin Martin Jose
Kevin Martin Jose

Reputation: 640

Focussing in Java swing

I'm developing an application using java swing. When I click a button, I want another window to be opened. This works fine. But it is possible to alt+tab to the original window and then interact with it even after the new window is open. Is there any way not to let the user focus the original window after the new window appears? By window I'm referring to Jframe/Jdialog.

Upvotes: 1

Views: 81

Answers (2)

user330315
user330315

Reputation:

Assuming the instance of your main JFrame window is called mainWindow:, the following code will prevent switching the focus.

// the second parameter makes the dialog modal and will prevent
// switching the focus to the mainWindow
JDialog dialog = new JDialog(mainWindow, true);
...
dialog.setVisible(true);

Documentation on JDialog: http://docs.oracle.com/javase/6/docs/api/javax/swing/JDialog.html

Upvotes: 3

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

You may try to use a JDialog instead of JFrame and pass the instance of JFrame to JDialog constructor

You may also try to check

 frame.setAlwaysOnTop(true);

or may be like this:-

frame.toFront();
frame.setState(Frame.NORMAL); 

Upvotes: 1

Related Questions