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:
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();
}