Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:08:50+00:00 2026-05-29T09:08:50+00:00

I have a problem showing my progress bar when reading a file in Java.

  • 0

I have a problem showing my progress bar when reading a file in Java.
All works as intended, user choose a file, the program must show the progress bar (but it loads an empty blank frame), process the file and then load the results on another window.

I can’t get the program to show the content of the progress bar dialog.
A little help here would be really appreciated.

Here is the code of the 3 methods involved.

//this method reads the file
public void processFile(File arch) {       
  aFile = arch;
  Thread threadForSearch = new Thread() {
  @Override
  public void run() {
      try{
         listaProveedoresTango = controladoraConsultas.traerProveedores();  
         listaProveedoresAFIP = new LinkedList();
     BufferedReader data = new BufferedReader(new FileReader(aFile));
     String s;
      while ((s = data.readLine()) != null) {                  
     //long task                 
      }
      data.close();
    }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }
      }
    };

    interfacesController.loadProgressBar();

    threadForSearch.start();         

   try {
      threadForSearch.join();
   } catch (InterruptedException ex) {
      Logger.getLogger(Controladora.class.getName()).log(Level.SEVERE, null, ex);
   }
   this.interfacesController.closeProgressBar();
   this.interfacesController.loadResults(someStuff);       
}

//load a progress bar
public void loadProgressBar(){           
  JProgressBar pb = new JProgressBar(0,100);
  pb.setPreferredSize(new Dimension(175,20));
  pb.setString("Processing Data");
  pb.setStringPainted(true);
  pb.setIndeterminate(true);
  JLabel infoLabel = new JLabel("Reading File: ");
  JButton cancelButton = new JButton("Cancel");
  cancelButton.addActionListener(new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent evt) {
      exitSystem();
    }
  });
  cancelButton.setVerticalAlignment(SwingConstants.CENTER);
  JPanel center_panel = new JPanel();
  center_panel.add(infoLabel);
  center_panel.add(pb);
  center_panel.add(cancelButton);
  center_panel.setLayout(new BoxLayout(center_panel,BoxLayout.Y_AXIS));
  dialog = new JDialog((JFrame)null, "Processing ...");
  dialog.getContentPane().add(center_panel, BorderLayout.CENTER);
  dialog.setSize(100, 100);
  dialog.setLocationRelativeTo(null);
  dialog.pack();
  dialog.setVisible(true);          
}

//close the open progress bar
public void closeProgressBar(){
   this.dialog.dispose();
}

Solved with SwingWorker, i post a summarized code:

public void processFile(File arch) {

    aFile = arch;

    final SwingWorker searchOnFile = new SwingWorker(){  

      @Override  
      protected Object doInBackground() throws Exception {  
        try{
            BufferedReader data = new BufferedReader(new FileReader(aFile));
            String s;
            while ((s = data.readLine()) != null) {                  
                //long task                  
             }
             data.close();
        }catch (Exception e){ //Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
     interfacesController.closeProgressBar();
     interfacesController.loadResults(someStuff); 
     return null;
     }
   };  

   interfacesController.showProgressBar(); 

   searchOnFile.execute();

}

interfacesController contains all the methods to work with GUIs, showProgressBar() is used to show the bar and closeProgressBar() do the opposite. Thank you guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:08:51+00:00Added an answer on May 29, 2026 at 9:08 am

    Short of more useful code, I suggest using a SwingWorker.

    An abstract class to perform lengthy GUI-interaction tasks in a background thread. Several background threads can be used to execute such tasks. ..

    Given the nature of the task, you might also look at ProgressMonitorInputStream.

    ..creates a progress monitor to monitor the progress of reading the input stream. If it’s taking a while, a ProgressDialog will be popped up to inform the user. If the user hits the Cancel button an InterruptedIOException will be thrown on the next read. All the right cleanup is done when the stream is closed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem for showing a full size div over all of my
I have a problem with validation messages not showing after a redirect, even when
I have a list showing up when I click on button, the problem is
The problem is quite basic. I have a JTable showing cached data from a
I have problem with return statment >.< I want to store all magazine names
I have the problem that the accelerators of JMenuItems aren't working anymore after showing
I have a problem with showing generated PDF. The pdf is saved in dataBase.
I have a problem with showing a <rich:modalPanel> more than once. I have a
UPDATE I tried Crazy's solution but now I have problem showing data in a
I have a problem where only some markers are showing and not others. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.