1

JInternalFrame을 확장하는 GameUpdater라는 자바 클래스가 있습니다. 클래스를 프로그램 자체로 실행했을 때 JFrame을 확장하는 데 사용했지만 JInternalFrame으로 변경하여 더 큰 응용 프로그램의 일부가되었습니다. 이제 메뉴 버튼을 통해 액세스 할 수 있습니다.JInternalFrame이 표시되지 않습니다.

I는 다음과 같이 메뉴 버튼이 누르면 함수가 호출되고 : 주기적 표시 할 JInternalFrame의 콘텐츠 업데이트되도록

private void update(){ 

    GameUpdater gu = new GameUpdater(); 
    desktop.add(gu); //add to JDesktopPane 
    gu.setSize(400, 300); 
    gu.setVisible(true); 

    gu.readMatches();//this function takes ages 

    gu.setMatch("Updating database...");//this is some output to the user, displays info in the internal frame 
    //try and insert into database 
    for(Match m : gu.getMatches()){ 
     db.insertMatch(m); 
    } 
    gu.setMatch("DONE"); //now it shows the frame, way too late 
} 

메소드 gu.readMatches()는, 실행하는 데 시간이 오래 걸린다는 진행. 그러나이 업데이트 기능이 완료 될 때까지 프레임이 표시되지 않습니다!

그것은는, setVisible처럼 (사실은)는 JFrame의 때

그것은 절대적으로 괜찮 았는데 ... 기능이 끝날 때까지 기다리고 있습니다. 이 원인이되는 JInternalFrame의 이상한 속성이 있습니까?

건배

답변

2

당신이 Event Dispatching Thread (EDT) 내부에 시간이 걸리는 과정을 실행하는 것처럼 그것은 소리는이 요청을 다시 칠 (다른 것들 사이) 공정에서 이벤트 큐를 방지 할 수 있습니다.

이렇게하면 "중단"된 것처럼 프로그램이 표시됩니다.

이 작업을 백그라운드 스레드에로드 해제해야합니다.

Concurrency in Swing을 통해 읽기, Worker Threads and SwingWorker

+0

워커 스레드가 해결했습니다. 건배! –

2

문제에 특히 섹션이 차단되도록되어 가지고 당신의 EDT이 방법 gu.readMatches();를 호출 단순히 새로운 Thread/Runnable 타르를 생성에 의해 처리 할 수 ​​있습니다

SwingUtilities.invokeLater(new Runnable() { 
@Override 
public void run() { 
gu.readMatches(); //gu will have to be declared `final` 

gu.setMatch("Updating database...");//this is some output to the user, displays info in the internal frame 
//try and insert into database 
for(Match m : gu.getMatches()){ 
    db.insertMatch(m); 
} 
} 
}); 

당연히 당신이이 독서 얼마나 멀리 의의 JProgressBar 때문에 사용자가 유지할 수 추적을 구현 할 수 있습니다하지만.

관련 문제