The following simple snippet grabs all the available screen devices in Java no problem:
GraphicsDevice[] gds = ge.getScreenDevices();
That much is simple. However, I’d like to get notified when this changes (an existing screen changes, one is removed, a new one is added, etc.)
I could just poll on the above method call which is do-able, but ideally I’d like a notification (observer pattern style) when something changes. I can’t see a way in the API to hook onto such an event though.
Is this possible, either through plain JDK or an add-on library, or should I just resort to polling every other second or so?
From all the looking around I’ve done, the answer at present (looking at Java 7 and the currently proposed Java 8 APIs) is simply no – you just have to poll it seems.
Although this is nasty from an OO perspective from a performance perspective it doesn’t really make a difference as long as you’re sensible with intervals – it seems to be a very cheap operation. My approach was to do the polling in a separate class and provide an observer pattern on which others could register. Does the job well, all the polling is in one place and if a better API comes along later, it’d be trivial to swap out with the new interface.