Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2007, 03:02 AM
Member
 
Join Date: Nov 2007
Location: Bay Area, CA
Posts: 13
Rep Power: 0
ibanez270dx is on a distinguished road
Default JPanel won't update
Hello,
Everything in my applet works except for one thing - When I click the JButton and the event takes place, it is supposed to change a JPanel to say "please wait", but instead, it freezes up for a few seconds while it completes the next commands and goes to the JPanel that is supposed to display after the commands are made, completly bypassing the "please wait" JPanel. The code looks like this:

Code:
 public void actionPerformed(ActionEvent pt)
     {
      if(pt.getSource() == init_Button)
	{
 	  overallLayout.show(cardPanel, "2"); // please wait JPanel
	  try
	    {
        // Construct data
         String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
         data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
    
	// Send data
         URL url = new URL("http://www.loaded-designs.com/posttest/posttest.php");
         URLConnection conn = url.openConnection();
         conn.setDoOutput(true);
         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
         wr.write(data);
         wr.flush();
    
       	// Get the response
       	 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
       	 String line;
       	 while ((line = rd.readLine()) != null) 
	   {
            getback_Label.setText("The PHP variables are below:");
            getback_Label2.setText(line);
	    System.out.println(line);
 	    overallLayout.show(cardPanel, "3");
           }
       	 wr.close();
         rd.close();
    	    }
	 catch (Exception ex)
	    {
	     ex.printStackTrace();
   	    }
        } 

     }
Does anyone know how to fix this? I tried repaint() but it didn't work... I'm really new at Java, so I think I might not have implemented it correctly. Any help is appreciated!

Thank you very much,
- Jeff
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-17-2007, 04:32 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
Swing works with the event dispatch thread (EDT). It is an attempt to make sure all events occur in proper sequence by having a single queue for all requests to wait in. So doing a time-consuming task like reading data over a network makes everything else wait until the long-running task completes. This has the net affect of freezing the gui and dis-allowing any gui updates to take place while the edt is busy with the long-running task. The solution is to do these long-running tasks on a background thread.
For example:
Code:
public void actionPerformed(ActionEvent pt)
{
    if(pt.getSource() == init_Button)
    {
        overallLayout.show(cardPanel, "2"); // please wait JPanel
        loadData();
    }
}

private void loadData()
{
    Thread thread = new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                // Construct data
                String data = URLEncoder.encode("key1", "UTF-8") + "=" +
                              URLEncoder.encode("value1", "UTF-8");
                data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" +
                              URLEncoder.encode("value2", "UTF-8");

                // Send data
                URL url = new URL("http://www.loaded-designs.com/" +
                                  "posttest/posttest.php");
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr =
                    new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();
    
                // Get the response
                BufferedReader rd = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) 
                {
                    getback_Label.setText("The PHP variables are below:");
                    getback_Label2.setText(line);
                    System.out.println(line);
                    overallLayout.show(cardPanel, "3");
                }
                wr.close();
                rd.close();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });
    // Keep gui responsive to user input.
    thread.setPriority(Thread.NORM_PRIORITY);  // 5, EDT = 6
    thread.start();
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-19-2007, 04:42 AM
Member
 
Join Date: Nov 2007
Location: Bay Area, CA
Posts: 13
Rep Power: 0
ibanez270dx is on a distinguished road
Default
hey, thanks hardwired, that helps alot!

However, it won't let me compile... it tells me that "public void run()" is an illegal start of expression... how can I fix this?

My code currently looks like this:
Code:
   private void loadData()
     {
      Thread thread = new Thread(new Runnable());
       {
        public void run()
        {
         try
            {
             // Construct data
             String data = URLEncoder.encode("key1", "UTF-8") + "=" +
                           URLEncoder.encode("value1", "UTF-8");
             data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" +
                           URLEncoder.encode("value2", "UTF-8");

             // Send data
             URL url = new URL("http://www.loaded-designs.com/" + "posttest/posttest.php");
             URLConnection conn = url.openConnection();
             conn.setDoOutput(true);
             OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();
    
             // Get the response
             BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
             String line;
             while ((line = rd.readLine()) != null) 
                {
                 getback_Label.setText("The PHP variables are below:");
                 getback_Label2.setText(line);
                 System.out.println(line);
                 overallLayout.show(cardPanel, "3");
                }
                wr.close();
                rd.close();
            }
          catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }

     // Keep gui responsive to user input.
     thread.setPriority(Thread.NORM_PRIORITY);  // 5, EDT = 6
     thread.start();
    }
   	   

  } 
}
I know this error is usually caused by nonmatching braces, but they all seem to be fine...

Thank you very much!
- Jeff

Last edited by ibanez270dx; 11-19-2007 at 04:53 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-06-2009, 08:59 PM
Member
 
Join Date: Jan 2009
Posts: 1
Rep Power: 0
ShaggyTDawg is on a distinguished road
Default Mismatched Braces
ibanez270dx:

By my count, you have one too many } . Though I'm hoping you figured that out a year ago...

Also, notice on the line :

Thread thread = new Thread(new Runnable()

that hardwired didn't close off the Thread() arguements on that same line. It shouldn't get closed off until right before you set thread priority toward the end. Essentially all of the code for the load Data is an argument for the new Thread(). That would be another issue with what you posted.



Last edited by ShaggyTDawg; 01-06-2009 at 09:08 PM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to update data for a JPA many-to-many relationship? abhijit.sarkar Enterprise JavaBeans 1 11-04-2008 08:48 AM
Using sql:update tag Java Tip Java Tips 0 01-13-2008 11:49 PM
Java Update Installation guest New To Java 1 12-01-2007 04:36 AM
Java Crashes on Mac 10.3.9 not sure how to update patricknowow New To Java 1 11-30-2007 03:57 AM
dynamic update in swt sandor SWT / JFace 0 05-14-2007 08:32 PM


All times are GMT +2. The time now is 04:18 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org