我需要从另一个线程更新GUI线程中的文本区域。我知道我必须使用invokelater。但是不知道如何应用。
这是我尝试更新GUI的地方
MinFrame.logWriter(MinFrame.logGrabber(mf.screen) + "\nDatabase Download Started bhgh",mf.screen);
这是Downloader类的完整代码(辅助线程)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jvazrfid;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Statement;
import javax.swing.SwingUtilities;
import org.json.JSONArray;
import org.json.JSONObject;
/**
*
* @author tmaxs
*/
public class Downloader implements Runnable {
public void run() {
LoginValidator lv = new LoginValidator();
String updateTime = lv.getupdateDb();
updateGUI("db", dataDownload(updateTime));
// Starting Images Download
}
public String dataDownload(String updateTime) {
MinFrame mf = new MinFrame();
String number = "";
try {
String url = "3000/protected/students?updatedAfter=" + updateTime;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Authorization", "BEARER "
+ "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7Im5hbWUiOiJJc3VydSBSdWh1bmFnZSIsInMTU1ODkzOTI0NiwiZXhwIjoxNTU4OTQ2NDQ2fQ.1Vi0TNPWUNgE2e-OXNfQj92_bfOXkfEBjtymW1qGiDY");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
// JSONObject objt = new JSONObject(response);
JSONArray array = new JSONArray(response.toString());
DownloadImage image = new DownloadImage();
System.out.println("Text : " + MinFrame.logGrabber(mf.screen));
MinFrame.logWriter(MinFrame.logGrabber(mf.screen) + "\nDatabase Download Started bhgh",mf.screen);
// SwingUtilities.invokeLater(mf.screen);
System.out.println("Database Download Started hhh");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String ImageStatus = image.ImageDownloader(object.getString("indexNo"));
try {
Statement s = DBL.getConnection().createStatement();
s.executeUpdate("INSERT INTO `students` (`active`, "
+ "`verified`, "
+ "`_id`, "
+ "`indexNo`, "
+ "`initials`, "
+ "`lastName`, "
+ "`fullName`, "
+ "`DOB`, "
+ "`gender`, "
+ "`address`, "
+ "`grade`, "
+ "`section`, "
+ "`medium`, "
+ "`homeTel`, "
+ "`imageDownloaded`) VALUES "
+ "('" + object.getBoolean("active") + "',"
+ "'" + object.getBoolean("verified") + "', "
+ "'" + object.getString("_id") + "', "
+ "'" + object.getString("indexNo") + "', "
+ "'" + object.getString("initials") + "', "
+ "'" + object.getString("lastName") + "', "
+ "'" + object.getString("fullName") + "', "
+ "'" + object.getString("DOB") + "', "
+ "'" + object.getString("gender") + "', "
+ "'" + object.getString("address") + "', "
+ "'" + object.getString("grade") + "', "
+ "'" + object.getString("section") + "', "
+ "'" + object.getString("medium") + "', "
+ "'" + object.getString("homeTel") + "', "
+ "'" + ImageStatus + "')");
Statement updateDate = DBL.getConnection().createStatement();
updateDate.executeUpdate("update updates set timestamp = '" + object.getString("updatedAt") + "' where id = 1");
number = Integer.toString(i);
DBL.getConnection().close();
} catch (Exception ex) {
ex.printStackTrace();
mf.logWriter(mf.logGrabber(mf.screen) + "\nDatabase error",mf.screen);
return "Database error";
}
mf.logWriter(mf.logGrabber(mf.screen) + "\nDatabase Download finished",mf.screen);
System.out.println("Database Download finished");
}
MinFrame.logWriter(MinFrame.logGrabber(mf.screen) + "\nNo values Retrieved",mf.screen);
/*System.out.println("base -" + myresponse.getString("base"));
System.out.println("date -" + myresponse.getString("date"));
JSONObject rates_object = new JSONObject(myresponse.getJSONObject("rates").toString());
System.out.println("rates -" + rates_object);
System.out.println("AUD -" + rates_object.getDouble("AUD"));
System.out.println("BGN -" + rates_object.getDouble("BGN"));
System.out.println("BRL -" + rates_object.getDouble("BRL"));
System.out.println("CAD -" + rates_object.getDouble("CAD"));*/
} catch (Exception e) {
System.out.println(e);
return "URL Parse Error";
}
return number;
}
private void updateGUI(String key, String val) {
if (key.equalsIgnoreCase("db")) {
System.out.println("here");
SpalshScreen screen = new SpalshScreen();
screen.databaseDownload.setText("Database Download successful - " + val + " rows downloaded");
}
}
}