JFrame Keep Focus

B

Ben

Hi,

Is it possible to make a JFrame keep focus no matter if the user tries
to click other frames? If so how do I do it? I was thinking of using
setFocusableWindowState on all other frames and setting them to false.
Is this the easiest way? Also is it possible to remove the x from the
top of the frame so it can only be closed using a JButton?

Thanks.

Ben.
 
V

VisionSet

Ben said:
Hi,

Is it possible to make a JFrame keep focus no matter if the user tries
to click other frames? If so how do I do it? I was thinking of using
setFocusableWindowState on all other frames and setting them to false.
Is this the easiest way? Also is it possible to remove the x from the
top of the frame so it can only be closed using a JButton?

Not sure if this is the best way but why not add a WindowFocusListener to
the frame you want to retain focus, then when it loses focus request the
focus again?

addWindowFocusListener(new WindowAdapter() {

public void windowLostFocus(WindowEvent evt) {
requestFocusInWindow();
}
});
 
B

Ben

In the end I found that I can used setEnabled(false) on the forms to
stop users clicking on them.

Will also use
addWindowFocusListener(new WindowAdapter() {

public void windowLostFocus(WindowEvent evt) {
requestFocusInWindow();
}

});

because if forms clicked on task bar they get focus even though they
can't be used.
 
B

Ben

Actually

addWindowFocusListener(new WindowAdapter() {

public void windowLostFocus(WindowEvent evt) {
requestFocusInWindow();
}

});

didn't work when used with setEnabled(false)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top