Reputation: 2141
I need to implement a routine that ensure that the application will never lose the focus for others applications (however when it is minimized I shouldn't force it come back). So I decided to implement WindowFocusListener in the main window:
public class DialogoPrincipal extends JFrame implements WindowFocusListener {
public DialogoPrincipal() {
initComponents();
this.addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent e) {
//Do nothing
}
@Override
public void windowLostFocus(WindowEvent e) {
this.toFront();
}
/*hidden code*/
}
It works great when the main window don't show any subwindows. But when some subwindows are opened, I can't force the focus for the application. Is there a way I can force the focus in the application, even when the application have subwindows, or I need implement WindowFocusListener in all of my dialogs? If I need to implement this interface, what can I do for JOptionPane.showMessageDialog(...) don't lose the focus to?
Upvotes: 1
Views: 165
Reputation: 2141
I solved it implementing an abstract class that is inherited by all subwindows:
public abstract class DialogoFocado extends JDialog implements WindowFocusListener {
public DialogoFocado(Frame owner) {
super(owner);
this.addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent e) {
}
@Override
public void windowLostFocus(WindowEvent e) {
this.toFront();
}
}
Upvotes: 1
Reputation: 109823
not possible to maintain Focus, FocusRecycle, FocusSubSystem, ZOrder betweens JFrames correctly, dirty hack have to add JFrames
to two_dimmensional array
, which is supply FocusWhatever
and with proper ordering
FocusWhatever
is simple asynchronous
, because is based on properties came from Native OS
, have to delay FocusWhatever
inside invokeLater
,
logic for Dialog Focus (AncestorListener) by @camickr (have to hold ancesors in Array) could be usefull for proper container hierarchy JFrame
& JDialog
use JDialog with Parent and proper ModalityType, then focus
could be returned back to the Parent
Upvotes: 3