JProgressBar doesn't update , can't find a clue

后端 未结 2 588
情书的邮戳
情书的邮戳 2021-01-20 18:47

nice job , now i just wanna know why if i add into while loop the instruction System.out.println below the progress is shown on both , cmd and Pgbar in the Gui ?? :

2条回答
  •  死守一世寂寞
    2021-01-20 19:25

    If you can't use SwingWorker then use SwingUtilities.invokeLater, e.g.:

    if (progress != Path.operationProgress) {
        final int progressCopy = progress; // Probably not final so copy is needed
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            void run() {
                operationsProgressBar.setValue(progressCopy);
            }
        });
    }
    

    Note: When doing this, everything used in run has to be final or there have to be other measures to access the variables. This code is symbolic in that regard.

    You need to do operations on Swing components outside the event dispatching thread, there is no way around this.

提交回复
热议问题