title: How to listen to graphic environment or device changes in java
body: <p>In java one can get the current graphics environment using</p>
  
  <pre><code>GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  </code></pre>
  
  <p>and than loop through the devices using</p>
  
  <pre><code>GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
  
  for (int i = 0; i &lt; screenDevices.length; i++) {
      GraphicsDevice graphicsDevice = screenDevices[i];
      ...
  }
  </code></pre>
  
  <p>But the number of <code>GraphicDevice</code>s can change at runtime, e.g. by plugging in an USB-monitor. Or the user can change a <code>GraphicsDevice</code>'s resolution or move it relative to another graphics device if there are multiple monitors.</p>
  
  <p>How can I listen to changes of the <code>GraphicsEnvironment</code> and the <code>GraphicDevice</code>s?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried to implement the change detection using a daemon thread that inspects the graphics environment at a specific interval, but it doesn't work.</p>
  
  <p>It doesn't work because the <code>GraphicsEnvironment</code> is hold in a statc variable and initialized only once.</p>
  
  <p>Also the sub classes of <code>GraphicsEnvironment</code> cache the <code>GraphicDevice</code>s. So new <code>GraphicDevice</code>s will only be detected if the jvm process is restarted.</p>