My application consists of two class files, let’s call them A and B. A is the main one – the one which is executed and the main class of the jar file. In A I define a JFrame, to which I add two JPanels: one of them is composed of JButtons and JLabels, and the other one is a graphical one, and is implemented in B (B extends JPanel).
In B I have a method which could take a while, so I wish to give the user information about the process going on there; namely, I’d like to tell the user how much computation is left. I want to do it by updating a JLabel in class A. However, when I try it, I get that the JLabel is updated only after the method had been completed, i.e. instead of seeing it running from 0% to 100% I only see 100%, all the time.
I don’t think I have any relevant pieces of code to show you, but if there’s something you’d want me to post, please do let me know.
I’ve checked similar questions but it appears to me that they asked something quite different.
Thank you,
Ron.
Yours is a threading problem. Your long-running process is tying up the main Swing thread, the EDT, preventing any drawing, updating of components, or user interactions. You need to do the long-running process in a background thread such as one supplied by a SwingWorker object. Do this and your GUI will become much more responsive.
Check out Concurrency in Swing for more on this important topic.
If you need more specific help, you will need to supply more information and code.