2013-07-09 25 views
4

我與一個允許使用指紋掃描器的JNI進行交互。我寫的代碼將掃描的ByteBuffer解析成JNI,並將其轉換爲BufferedImage進行保存。在線程中獲取和設置標誌變量

我無法弄清楚的是如何在我的GUI上的jlabel圖標嘗試更新之前等待掃描線程完成。最簡單的方法是什麼?

我還需要添加什麼?

編輯:

//Scanner class 
Thread thread = new Thread() { 
     public void run() { 
      // [...] get ByteBuffer and Create Image code 
      try { 
       File out = new File("C:\\Users\\Desktop\\print.png"); 
       ImageIO.write(padded, "png", out); 
       // [???] set flag here 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
thread.start(); 
return true; 

//Gui class 
private void btnScanPrintActionPerformed(java.awt.event.ActionEvent evt) { 
    Scanner scanPrint = new Scanner(); 
    boolean x = scanPrint.initDevice(); 
    //Wait for the scanning thread to finish the Update the jLabel here to show 
    //the fingerprint 
} 
+0

你可以顯示掃描線程內運行的代碼(的相關部分)嗎? – mthmulders

回答

3

不確定您是否在使用Swing或Android進行UI,但是您想要通知主事件派發線程(在swing中它就是這樣調用的)。您將運行掃描線程,然後在完成時向EDT發送「消息」,並執行您想對按鈕執行的操作。

Thread thread = new Thread(new Runnable(){ 
    public void run(){ 
     //scan 
     SwingUtiltilies.invokeLater(new Runnable(){ 
       //here you can update the the jlabel icon 
       public void run(){ 
        jlabel.setText("Completed"); 
       } 
     }); 
    } 
}); 

在用戶界面開發中,不需要等待某個操作完成,因爲您總是希望EDT能夠響應。

+0

+12簡而言之,當GUI等待時,它不會更新屏幕並凍結。 –

+0

謝謝! :D最後,我想我愛你......沒有同性戀。但是非常感謝你。我一直堅持這幾天。我試圖同步線程並創建自己的互斥類。這很簡單。現在我只需調整圖標大小,謝謝 – DeanMWake

+1

@MethodMan酷。然後接受答案。 :) – zEro

0

在掃描線程結束,使用SwingUtilities.invokeLater()更新與掃描結果的GUI。

相關問題