Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: ... is not attached to a vertical group


Swing을 이용해 여려 쓰레드에서 동시에 GUI작업을 하면 이런 에러가 발생한다.



EventQueue 클래스는 시스템에서 발생하는 이벤트 를 Queue 형태로 관리해 준다. 응용 프로그램 하나에는 이러한 EventQueue도 하나만 존재하게 된다. 응용 프로그램에서 발생하는 모든 이벤트는, 이벤트를 처리하는 쓰레드가 내부에 존재하여 처리 하고 있다.


해결방법 : EventQueue 클래스의 쓰레드 생성이용


이름

 static void invokeAndWait(Runnable runnable)

 해설

 이벤트 디스패칭 쓰레드에 의해 Runnalbe.run()을 실행한다. invokeAndWait 메소드에서 제어가 돌아오는 것은 이벤트 큐에 등록된 모든 이벤트가 처리되고 runnable.run()의 실행이 종료되고 난 다음이다.

 이름

 static void invokeLater(Runnable runnable)

 해설

 이벤트 디스패칭 쓰레드에 의해 Runnalbe.run()을 실행한다. 이 메소드를 호출하면 이벤트 큐에 runnable.run()의 내용을 넣고 바로 리턴한다.

 이름

 static boolean  isEventDispachThread()

 해설

 현재의 쓰레드가 이벤트 디스패칭 쓰레드인지 여부를 판단한다.

표 출처 : http://blog.naver.com/xxrcn11?Redirect=Log&logNo=20130278668


예시 )

public class MyPanel1 extends JPanel implements Runnable{

...

     public void run(){

          ...
     }

}



//쓰레드 생성시

MyPanel1 panel1 = new MyPanel1();


//기본적인 쓰레드 생성

/*

Thread t1 = new Thread(panel1);

t1.start();

*/

//위를 사용 하는 대신 사용할 것


EventQueue.invokeAndWait(panel1);


자세히 알고 싶으신 분은

여기로 가시면 됩니다 http://blog.naver.com/xxrcn11?Redirect=Log&logNo=20130278668



by 개발자가 되자! 2013. 5. 24. 01:03
| 1 |