Search code examples
javamultithreadingswingjprogressbar

How to use a JProgressBar in a different Thread?


I'm using POI in a function to fill the content of my excel document (it takes 10 seconds) and when i call my function I want to use a JProgressBar to see the progress, but the button and the program is block, i need to to make in other thread? and how can I do it? an example of my code:

btnEjecutar.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
        //the function
        generarDocumento(nombre);
            }

Solution

  • Try to use an invokeLater, like the example bellow:

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            generarDocumento(nombre);
        });