开发者

In Java, Can I have a routine called every time the event-dispatch-thread returns from handling an input event?

开发者 https://www.devze.com 2023-03-04 01:15 出处:网络
I am working on a GUI, and I have a routine to update the display when things chan开发者_如何学Cge underneath:

I am working on a GUI, and I have a routine to update the display when things chan开发者_如何学Cge 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?


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


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消