erikb

Prev Next TECH TECH
Java: checking Swing thread calls this [2004-08-26]

This repaint manager will make sure you call swing repaint operations from the right thread (the event dispatching thread).

From ClientJava: Easily Find Swing Threading Misakes

Not a bad idea, simple and easy to modify for your own needs.

//
RepaintManager.setCurrentManager(new ThreadCheckingRepaintManager());
//
public class ThreadCheckingRepaintManager extends RepaintManager {
  public synchronized void addInvalidComponent(JComponent jComponent) {
    checkThread(jComponent);
    super.addInvalidComponent(jComponent);
  }

  private void checkThread(JComponent c) {
    if (!SwingUtilities.isEventDispatchThread() && c.isShowing()) {
      System.out.println("Wrong Thread");
      Thread.dumpStack();
    }
  }

  public synchronized void addDirtyRegion(JComponent jComponent, 
                                          int i, int i1, int i2, int i3) {
    checkThread(jComponent);
    super.addDirtyRegion(jComponent, i, i1, i2, i3);
  }
}
Prev Next