0

I have a program that should issue some action when it gets activated after loosing focus to some other application before. I wrote a Focus-Listener to achieve this:

    frame.addWindowFocusListener(new WindowFocusListener() {
        @Override
        public void windowGainedFocus(WindowEvent e) {
          <do something when we gain focus>
        }

        @Override
        public void windowLostFocus(WindowEvent e) {
          <do something else when we lose focus>
        }
    });

Problem is, the gain-focus-action is also called when a modal dialog (for instance an authentication dialog) closes - the main window gains focus again. So I somehow would need to detect, if I gain focus from within my program or from the outside. Or, differently put, the focus action should not be located on the main window but on the application itself. What would be a simple way to do this?

sers
  • 3,139
  • 3
  • 20
  • 28
  • please to [see my question about](http://stackoverflow.com/q/10880326/714968) for more ... – mKorbel Dec 13 '16 at 13:43
  • *"I have a program that should issue some action.."* What (specifically)? See [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Dec 13 '16 at 13:47
  • @AndrewThompson: reload data. it could be anything. does it matter? my problem is, it doesn't happen at the right time. – sers Dec 13 '16 at 14:12
  • 1
    By *"reload data"* DYM *"update / refresh data"* becuase if not, and you actually mean *"reload data"* then the better strategy is not to lose the data in the first place. *"does it matter?"* Possibly, if you have the entire wrong understanding about the best way to implement an app. This request has that 'bad smell'. – Andrew Thompson Dec 13 '16 at 14:28
  • 1
    use this `e.getOppositeWindow()==theDialogYouWantToIgnore` to ignore the focus gained event if the previous window was that dialog – Aelop Dec 13 '16 at 14:53
  • @AndrewThompson, "refresh data" to phrase it correctly. The problem is, that the application could potentially remain inactive for a long time, and when it gains focus again, it needs to refresh. Multi user database, so data probably have changed ... – sers Dec 13 '16 at 15:11
  • 1
    OK.. but. let's say I had an app. showing stock prices that only takes up 1/3 of the screen. I might have it open while working on another document in a word processing program on another part of the screen. The word processing program has focus, but I'd still want the stock price program to be updating. I cannot say for sure that your user would or would not want the app. to be refreshing data in that circumstance, but they **might** prefer that it only 'rests' at updating data if minimized or if they explicitly order it to 'pause'. – Andrew Thompson Dec 13 '16 at 15:33
  • @AndrewThompson Could be that way. But the queries are not especially cheap and our goal is not to increase load to the database if it's not necessary. So it's kind of a compromise for the time being. – sers Dec 14 '16 at 14:25

2 Answers2

2

Thanks @Aelop for helping me find an answer. e.getOppositeWindow() is null for windows of other applications, so I can neatly distinguish from where I'm coming from:

    frame.addWindowFocusListener(new WindowFocusListener() {
        @Override
        public void windowGainedFocus(WindowEvent e) {
            if (e.getOppositeWindow()==null) {
                <do something when we gain focus>
            }
        }

        @Override
        public void windowLostFocus(WindowEvent e) {
          <do something else when we lose focus>
        }
    });
sers
  • 3,139
  • 3
  • 20
  • 28
0

if you didn't find a good solution use static variable when the focus is lost from a dialog in your application set a static boolean variable to true so when the focus is gained and the variable is true means that the focus is from some window in the application else the focus is from somewhere else i hope you get the idea try it if you didn't find some application or check the type of the source who lost the focus

Charif DZ
  • 14,415
  • 3
  • 21
  • 40