Search code examples
javaswingjframejprogressbar

Why does the second JFrame does not show components?


I have the first JFrame and it works fine. When I push a button it is supposed to show a JProgressBar frame , but i get empty JFrame. I open it with

                p = new Progress("1/3");
                p.setMax(2);
                p.setProgress(0, "Getting bytes...");

Anyone know why?

EDIT: I am going to explain more detail(Because someone misunderstood and corrected my post in the wrong way) - On my main class i start the first JFrame: new Crypt(); And in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty:

                p = new Progress("1/3");
                p.setMax(2);
                p.setProgress(0, "Getting bytes...");

The Progress class Screen shot


Solution

  • in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty

    Code invoked from an Swing listener executes on the Event Dispatch Thread (EDT). The EDT is responsible for painting Swing components. Since your code is executing a long running task on the EDT y9ou are preventing Swing from painting the component until the task is finished.

    You need to start a separate Thread for your long running task. Or better yet you should probably be using a SwingWorker. Read the section from the Swing tutorial on Concurrency in Swing which explains this in more detail and provides a working example of a SwingWorker.