title: Swing Timer and poor performance (Java, Swing)
body: <p>I have a problem regarding a <strong>Swing Timer</strong>. I'm trying to create a "sliding" animation for a <strong>JPanel</strong> that comes sliding in from the side when pressing a button. This actually works fine, but when I want it to slide out again, I am hit by extremely poor performance. Note: the JPanel is in a <em>JLayeredPane</em> and there are other components beneath it (it that matters at all). </p>
  
  <p>Here is the code for slide in:</p>
  
  <pre><code>int boundsTo = GlobalValue.getLargeInformationBarWidth();
          final int[] boundsNow = {0};
          inSlideTimer = new Timer(1, ae -&gt; {
              if (boundsTo &gt; boundsNow[0]){
                  boundsNow[0] = boundsNow[0] + 3;
                  JourneyPlannerBarController.getInstance().getInformationBar().setBounds(0, 0, boundsNow[0], window.getFrame().getHeight());
                  JourneyPlannerBarController.getInstance().getInformationBar().revalidate();
                  JourneyPlannerBarController.getInstance().getInformationBar().repaint();
              } else {
                  inSlideTimer.stop();
                  inSlideTimer = null;
              }
          });
          inSlideTimer.start();
          JourneyPlannerBarController.getInstance().setupLargeJourneyPlannerBar();
  }
  </code></pre>
  
  <p>and here is the code for the slide out:</p>
  
  <pre><code>int boundsTo = 0;
  final int[] boundsNow = {GlobalValue.getLargeInformationBarWidth()};
  inSlideTimer = new Timer(1, ae -&gt; {
          if (boundsTo &lt;= boundsNow[0]) {
              boundsNow[0] = boundsNow[0] - 3;
              JourneyPlannerBarController.getInstance().getInformationBar().setBounds(0, 0, boundsNow[0], window.getFrame().getHeight());
              JourneyPlannerBarController.getInstance().getInformationBar().revalidate();
              JourneyPlannerBarController.getInstance().getInformationBar().repaint();
  
          } else {
              inSlideTimer.stop();
              inSlideTimer = null;
              JourneyPlannerBarController.getInstance().clearJourneyPlannerBar();
              CanvasController.repaintCanvas();
          }
  });
  inSlideTimer.start();
  }
  </code></pre>
  
  <p>As can be seen, the two chunks of code are almost identical, but the performance difference really is quite something. I have been trying to read up on SwingWorker, EDT and more, but I have so far not found anything that can help me.</p>