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

  • SEARCH
  • Home
  • 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 612561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:54:25+00:00 2026-05-13T17:54:25+00:00

I wrote a simple application and I want show delay of it with JProgressBar

  • 0

I wrote a simple application and I want show delay of it with JProgressBar Plese help me ;

I want show JProgressBar with Joptionpane , with a cancel button and it should be modal

this is my source code :

class CustomFrame extends JFrame {

  private JProgressBar progressBar;

  public CustomFrame() {
    long start = System.currentTimeMillis();
    myMethod();
    this.getContentPane().setLayout(null);
    this.setSize(200, 200);

    //JOptionPane. ?????

    this.setTitle("JFrame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
       long end = System.currentTimeMillis();
        System.out.print("\nTime: " + (end - start));
  }
    public void myMethod(){
        try {
                     java.io.File file = new java.io.File("i://m.txt");
                     BufferedReader input =
                       new BufferedReader(new FileReader(file));
                     String line;
                     while ((line = input.readLine()) != null) {
                         if (line.indexOf("CREATE KGCGI=") != -1 ){
                             System.out.println(line);
                         }
                     }
                     input.close();
                   }
                   catch(Exception e){
                       e.printStackTrace();
                   }
    } 

Thanks …

  • 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-13T17:54:26+00:00Added an answer on May 13, 2026 at 5:54 pm

    There are a couple things that you will need to do to get this to work:

    1. You should be aware of threading issues in Swing. Your GUI painting should be done on the EventDispatchThread and disk I/O should be done in a worker thread. See this tutorial, the SwingWorker JavaDoc, and SwingUtilities.invokeLater for more detail
    2. You will then want to get the size of your file (file.length())to determine how to scope your progress bar (myProgressBar.setMaximum(length))
    3. When you iterate over the lines in your file, you will want to trigger an update to your progress bar (myProgressBar.setValue(myProgressBar.getValue()+lineLength)).

    A couple points by way of critique:

    • your constructor shouldn’t go off and do all of your work (ie load your file and pop up an option pane with the ability to cancel. the constructor should just do the work needed to create the object. you might want to consider having your constructor create your class, and then have the work that needs to be done to be called separately, or within something like an init() method.
    • It isn’t clear what you are doing with the JFrame as superclass. JOptionPane is a class that will pop up a very basic modal dialog with some text, maybe an icon or input field. It isnt a panel that is embedded in a dialog.
    • As JOptionPane is a very basic construct for creating a basic message dialog, it might be easier to use a JDialog, which can also be made modal. JDialog will allow you to add buttons as you please, where as a standalone JOptionPane will require you to use Yes/No, or Yes/No/Cancel or OK/Cancel etc.
    • If you still want to use JOptionPane, and only show a cancel button, you can instantiate a JOptionPane (as opposed to using the utility show* methods), with the progressbar as the message, and the JOptionPane.CANCEL_OPTION as the optionType param. You will still need to put this into a JDialog to make it visible. See this tutorial for more details:

    JOptionPane (constructor)

    Creates a JOptionPane with the specified buttons, icons, message, title, and so on. You must then add the option pane to a JDialog, register a property-change listener on the option pane, and show the dialog. See Stopping Automatic Dialog Closing for details.

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

Sidebar

Related Questions

I wrote a simple counter servlet application to show number of users who visit
I wrote a simple java application, I have a problem please help me; I
I have got a Wavecom Supreme GSM modem. I wrote a simple application that
I wrote a simple WinAPI application in C++ which embeds the Abode Flash ActiveX
I wrote some sort of console client for a simple application. To be more
I wrote a C# application that is a simple countdown timer. I use it
I wrote a simple Tkinter based Python application that reads text from a serial
I wrote a simple UpDown UserControl for my application. I am rendering it in
I want to produce a simple enough application which uses a QTreeView widget to
I want to write simple application with some 3D objects for Windows OS. The

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.