0

I am working on a GUI, and I have a routine to update the display when things change underneath:

void update() {
    if (needsUpdating) {
        // ...
        needsUpdating = false;
    }
}

I'm trying to avoid calling update() "too often" -- ie, if many properties are set in succession I'd rather update() be called just once.

Is it possible to have update() called after every user input event -- key/mouse/etc? I could do this manually, but I have so many event handlers and I know I'll forget -- can Java do this for me?

Owen
  • 38,836
  • 14
  • 95
  • 125

2 Answers2

1

yes, you can globally listen to user-events, though I wouldn't recommend it, except if you don't find another way:

http://tips4java.wordpress.com/2009/08/30/global-event-listeners/

The real problem seems to be your application design:

I could do this manually, but I have so many event handlers and I know I'll forget

try to model those "many" into separate parts and clearly define which part need to trigger an update at which time. Actually, there's no way around such a model, whatever the implementation of the actual listening, once you are beyond the most trivial of applications. For starters, see f.i.

https://softwareengineering.stackexchange.com/questions/71022/what-is-good-programming-practice-for-structuring-java-project

Community
  • 1
  • 1
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • You're probably right. The way I was thinking, the actual not-implementation condition I am looking for is "user did something", and I was hoping not to duplicate the condition "user did something" if it already existed. – Owen May 09 '11 at 11:48
0

You can use your own event queue, by subclassing EventQueue. With it control all the events, and do your updates when you want. See How to replace the AWT EventQueue with own implementation.

But I don't understand your use case : swing makes gui updates for all events, why do you want do your own ?

Community
  • 1
  • 1
Istao
  • 7,425
  • 6
  • 32
  • 39