DeanMWake
DeanMWake

Reputation: 923

Getting and Setting a Flag variable in a thread

I am interfacing with a JNI that allows me to use a fingerprint scanner. The code I have written takes the scanned ByteBuffer parsed back to it by the JNI and turns it into a BufferedImage for saving.

What I can't figure out is how to wait for the scan thread to finish before the the jlabel icon on my GUI tries to update. What would be the easiest way to do this?

What else do I need to add?

Edit:

//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
} 

Upvotes: 4

Views: 568

Answers (2)

John Vint
John Vint

Reputation: 40256

Not sure if you are using Swing or Android for UI but you would want to notify the main event dispatch thread (in swing it is called just that). You would run the scanning thread, then when complete send a 'message' to the EDT with the action that you want to be done to the button.

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");
              }
         });
     } 
});

In UI development there is no waiting for an action to complete because you always want the EDT to be responsive.

Upvotes: 3

jtahlborn
jtahlborn

Reputation: 53694

at the end of a the scan thread, use SwingUtilities.invokeLater() to update the gui with the results of the scan.

Upvotes: 0

Related Questions