-1

Possible Duplicate:
JDialog Stops execution of parent JFrame

I have to JFrame and it has undecorated and has only an image of progress bar in it...but when i want send request to the server i want it to be show progress bar and it is not showing it instead but my window is loaded....i have tried jDialog but it is stopping execution of my code..

here is my code

private void LoginBtnActionPerformed(java.awt.event.ActionEvent evt) {
    ProgressBar pb = new ProgressBar();
    pb.setVisible(true);
    List<BasicNameValuePair> postPairs = new ArrayList<BasicNameValuePair>();
    AsyncService asyncService = new AsyncService();

    postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc"));
        postPairs.add(new BasicNameValuePair("user_email",email));
        postPairs.add(new BasicNameValuePair("user_password",password));


        JSONArray jArray = asyncService.sendRequest(postPairs);
        pb.setVisible(true);
}

I have nothing in my progress bar frame except an gif animation image and a Label... EDIT 1 */

  class WorkerProgressBar extends SwingWorker<Integer, Integer>
  {
    private JFrame pb = new JFrame();
    JDialog dialog = new JDialog();
     JPanel panel = new JPanel();
     JLabel label = new JLabel();
     public WorkerProgressBar(){
             panel = new JPanel();
                panel.setBackground(new Color(114,114,114));
                JLabel imageLabel = new JLabel(new ImageIcon(getClass().getResource("/icons/progress_bar.gif")));
                imageLabel.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(imageLabel);
                dialog.getContentPane().add(panel,BorderLayout.CENTER);

                dialog.pack();
                dialog.setLocationRelativeTo(null);
     }
     protected Integer doInBackground() throws Exception
 {
        dialog.setVisible(true);
        return 0;
     }
     protected void done()
{
    try
    {
        dialog.setVisible(false);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}   
Community
  • 1
  • 1
Haseeb Wali
  • 1,181
  • 3
  • 14
  • 34

1 Answers1

4

You should use a JDialog if the window is to be above another Swing top level window, but must be sure that all code to be run is called before setting a modal dialog a visible. Key though is that you should be running long-running tasks on a background thread such as that provided by SwingWorker. There are many examples of this on this site as yours is a question that has been asked many times here.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    You mean like [this one](http://stackoverflow.com/a/14064101/418556)? ;) – Andrew Thompson Dec 31 '12 at 12:20
  • 1
    @Andrew: Or [this one](http://stackoverflow.com/a/10240173/522444) and [this one](http://stackoverflow.com/a/5533581/522444) – Hovercraft Full Of Eels Dec 31 '12 at 12:25
  • i have done it in swing worker but it is showing me white frame..and if i do't send async request than it is showing me proper progress image..? – Haseeb Wali Dec 31 '12 at 12:43
  • @HaseebWali: `"I have done it in swing worker but..."` -- so you are doing it wrong then, but we have no idea what you're doing wrong til you show us code. – Hovercraft Full Of Eels Dec 31 '12 at 12:48
  • @Haseeb: You need to read the [SwingWorker tutorial](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) and look at the examples we've linked to above as you're using it completely backwards for one by calling Swing from the doInBackground method and not calling your long-running task from this method. – Hovercraft Full Of Eels Dec 31 '12 at 13:08
  • @HaseebWali: I already showed you how to use a SwingWorker in Andrew's link above. Why re-ask the same question? Why try to help you if you simply ignore help and repeat the question? Haven't you read the tutorial that we linked to previously? Voting to close this current question as a waste of our time. If you're not willing or able to put in the effort then don't waste your time trying to learn to program. – Hovercraft Full Of Eels Dec 31 '12 at 13:27
  • yup i am implementing this and it seems to me that i will work for me it will take some time..thanks by the way...!\ – Haseeb Wali Dec 31 '12 at 13:32