Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing application problem

I'm developing Swing application, and everything works fine usually. But I have an GUI issue.

When I run the application, and for example minimize some other window, my application is still working, but the central part of JFrame is invisible or hidden. After finishing some part of program logic the GUI repaints and is visible again. This will continue until the program finishes running.

Is there any API for preventing this behavior, or some class for forcing the GUI visible, or maybe to add some progress bar?

If someone need this information I'm testing this on Windows Vista, with java 1.6.

like image 903
vaske Avatar asked Apr 08 '26 09:04

vaske


2 Answers

It sounds to me like you are doing some sort of slow IO or calculations that are causing your GUI to become unresponsive.

What you need to do is do the long running processes in another thread.

The standard way of doing that is with a SwingWorker class.

The Java tutorials have some great resources and tutorials on how to properly use the SwingWorker.

Here and here is a link to another question that I believe may be similar.

This behavior you are seeing is a result of your long running process blocking the thread that the GUI uses to repaint itself. Thus, while your task is performing, the GUI is unable to repaint your JFrame and it becomes unresponsive.

like image 114
jjnguy Avatar answered Apr 10 '26 23:04

jjnguy


Just like jinguy says you are most likely doing a task that takes a long time to complete and needs to be put in a background thread. Have a look at the Java Sun Concurrency in Swing Tutorial for more information on how to do this correctly.

like image 38
willcodejavaforfood Avatar answered Apr 10 '26 21:04

willcodejavaforfood