Swingworker does not enter the Propertychangelistener

Asked

Viewed 33 times

0

I created a swingworker to display a Jdialog while the code stands still waiting for the user to put his finger in the fingerprint reader

Everything worked right, I set as visible inside the Propertychangelistener and the method of harvesting the digital inside the doInBackground.

The problem is that when I put the doInBackuground return a value (which is a string returned by the method of harvesting the biometrics for me to store in the BD) it does not trigger the STARTED event for the Propertychangelistener. When you enter it is already in DONE, then the setvisible(true) of Jdialog is not reached.

Below is the code of my Swingworker:

public class Loader extends SwingWorker<Object, Object> {

    private JDialog dialog;


    public Loader(Frame owner) {
        dialog = new JDialog(owner, "Loading", true);
        dialog.setUndecorated(true);
        JLabel label = new JLabel( new ImageIcon("/opt/workspace/openbravo2/src-beans/com/openbravo/images/fingerinsert.png") );
        dialog.add( label );
        //dialog.add(progressPane);
        dialog.pack();
        dialog.setLocationRelativeTo(owner);
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

        addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("state".equals(evt.getPropertyName())) {
                    if (getState() == StateValue.STARTED) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                if (getState() == StateValue.STARTED) {
                                    dialog.setVisible(true);
                                }
                            }
                        });
                    } 
                } 
            }
        });
    }

    @Override
    protected Object doInBackground() throws Exception {
        return getFingerPrint();
    }

    @Override
    protected void done() {
        dialog.dispose();
    }

}

And down here the call:

Loader loader = new Loader(null);
loader.execute();

try {
    System.out.println(loader.get());
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

So as I said, if I don’t put the get() section then everything works right, but with it it doesn’t trigger the start event (which I understand is when the Ackground starts running).

Thanks in advance for the help.

  • 1

    I don’t understand: what has to be done in the background you put in doinbackground, and when you pass some value to the main thread, you go through the method done();. Behold here for example.

  • The code has been broken, I don’t know why.. I’m editing it. But in addition to your comment @Diegofelipe, the done() method returns void, in fact it "returns the return" of doInBackground.

  • This getFingerPrint() method will open the reader, await the collection of the digital and will return a String, which is the digital converted to be stored in the BD. So it stays in the background because there is a moment when it "hangs" the execution, which is when it turns on the reader’s light and waits until the user puts his finger there or expires the time.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.