Search code examples
javaswingswingworker

What are the type parameters in SwingWorker?


I have the following code to do a login process in the background:

private class LoginThread extends SwingWorker<Boolean, Object> {

        private Controller controller;
        private String userName;
        private String password;

        public LoginThread(Controller controller, String userName, String password) {
            this.controller = controller;
            this.userName = userName;
            this.password = password;
        }

        @Override
        protected Boolean doInBackground() throws Exception {
            status.setText("Try to log in user " + userName + "...");
            return controller.login(userName, password);
        }

        @Override
        protected void done() {
            try {
                if (get()) {
                    controller.loginDone();
                } else {
                    showErrorMessage("Can't login user " + userName + ".");
                }
            } catch (Exception ignore) {
                showErrorMessage("Can't login user " + userName + ".");
            }
        }

    }

I can´t find in the API an explanation what the second type parameter of SwingWorker is. The first type is probably the type that is returned by the doInBackground method, but what is the second type?


Solution

  • As stated on SwingWorker docs:

    java.lang.Object
        javax.swing.SwingWorker<T,V>
    
    Type Parameters:
    
        T - the result type returned by this SwingWorker's doInBackground and 
            get methods
        V - the type used for carrying out intermediate results by this SwingWorker's
            publish and process methods