views:

221

answers:

1

Hello guys

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 ...

+3  A: 

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.

akf