I am working on a java application which has a login form in a jframe. I have text fields and buttons in it.
The login button has an eventlistener which is an inner-class of the class that creates the login window. When user presses the login button, the listener takes the values form the fields and passes it to a validator which validates it using a mysql database and returns true and false based on the input by user. Now based on the return value the listener updates the ui using the if-else statement. This whole thing is working is fine.
The problem is that when the validation is being carried out the gui cannot be used, because every thing is being done with a single thread. So for that time the gui is kind of freezed.
How can I use multithreading to avoid this problem and use other gui components while validation is carried out.
As you’re probably aware, you should never perform long running tasks within the Event Dispatching Thread, this makes you program look like its hung.
Equally, you should never create/modify any UI component outside the Event Dispatching Thread.
One of the simplest solutions would be to use a SwingWorker. This allows you execute code in a background thread, but it will automatically resync it’s results back the Event Dispatching Thread…
Take a look at Concurrency in Swing for more information, in particular, SwingWorker