brevleq
brevleq

Reputation: 2141

Mantaining focus in application even when it have subwindows

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

Answers (2)

brevleq
brevleq

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

mKorbel
mKorbel

Reputation: 109823

Upvotes: 3

Related Questions