Ich habe eine GUI gebaut, welche mir den Wetterbericht für Karlsruhe liefert. Nun ist es so, dass ich das auf mehrere Städte erweitern will, sodass ich eine ComboBox habe, wenn ich da statt dem Defaultwert Karlsruhe Stuttgart wähle, dann passiert nichts, da im ActionListener kein Code ist. Ich habe alles mögliche schon versucht, es ist mir aber nicht gelungen, nach einer Änderung der ausgewählten Stadt den richtigen Wetterbericht mir anzeigen zu lassen, d. h. irgendwie ein "refresh" hinzbekommen.
Daher meine Frage - kann mir eventuell jemand freundlichen Weise einen Tipp geben, wo ich bei einer solchen Sachlage den Hebel ansetzen soll?
Daher meine Frage - kann mir eventuell jemand freundlichen Weise einen Tipp geben, wo ich bei einer solchen Sachlage den Hebel ansetzen soll?
Java:
package webserive.client;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import org.json.JSONObject;
import org.json.XML;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class Main {
private static JFrame mainFrame;
private static JComboBox<String> orte;
private static JPanel controlPanel;
private static int dateChanges = -1;
/**
* Extrahiere die Daten und uebergib diese der GUI-Bau-Methode.
* @param args
*/
public static void main(String[] args) {
try {
String output = getOutput();
TreeMap<String, String[]> forecasts = new TreeMap<>();
String forecast = ""; String dateOfForecast = "";
String[] lists = output.split("<list>");
String datum = ""; TreeSet<String> differentDates = new TreeSet<>();
for(int i = 1; i<lists.length;i++) {
forecast = lists[i].replace("</list>", "");
dateOfForecast = getInformationFor("DATEOF", forecast);
String[] infos = { getInformationFor("DESCRIPTIONOF", forecast),
getInformationFor("TEMPOF", forecast),
getInformationFor("HUMIDITYOF", forecast),
getInformationFor("PRESSUREOF", forecast),
getInformationFor("WINDSPEEDOF", forecast)
};
if(!datum.equals(dateOfForecast.substring(0, 10))) {
datum = dateOfForecast.substring(0, 10);
differentDates.add(datum);
dateChanges++;
}
forecasts.put(dateOfForecast, infos);
}
createWindow(forecasts, dateChanges, differentDates);
orte.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @return Der Output.
* @throws IOException
*/
private static String getOutput() throws IOException {
orte = new JComboBox<>();
String[] description = { "Karlsruhe", "Stuttgart" };
for (int i = 0; i < description.length; i++) {
orte.addItem(description[i]);
}
String output = ""; //Files.readString(Paths.get("C:\\Users\\*********\\Documents\\weatherdata.xml"));
output = XML.toString(readService(orte.getSelectedItem().toString()));
return output;
}
/**
* Hier wird die GUI gebaut.
* @param forecasts
* @param dateChanges
* @param differentDates
*/
private static void createWindow(TreeMap<String, String[]> forecasts, int dateChanges, TreeSet<String> differentDates) {
mainFrame = new JFrame("Wetterbericht");
mainFrame.setSize(750,500);
mainFrame.setLayout(new GridLayout(3, 1));
JLabel headerLabel = new JLabel();
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.setLayout(new FlowLayout(FlowLayout.LEFT));
pan.add(headerLabel); pan.add(orte);
mainFrame.add(pan);
AuxMethods auxMethods = new AuxMethods();
headerLabel.setText(auxMethods.getCurrentDate());
headerLabel.setBorder(new EmptyBorder(0, 10, 0, 0));
String[] columnNames = {"Voraussage für", "Beschreibung", "Temperatur", "Luftfeuchtigkeit", "Luftdruck", "Wind"};
Object[][] data = new Object[dateChanges+forecasts.size()][6];
int i = 0; int c = 0;
for(String d : forecasts.keySet()) {
String[] sa = new String[] { d,
forecasts.get(d)[0],
forecasts.get(d)[1],
forecasts.get(d)[2],
forecasts.get(d)[3],
forecasts.get(d)[4] };
data[i] = sa;
if(!d.substring(0, 10).equals(differentDates.toArray()[c])) {
data[i] = new String[] {"","","","","",""};
i++;
data[i] = sa;
c++;
} else {
}
i++;
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
mainFrame.add(scrollPane);
ImageIcon icon = new ImageIcon("pix/icon.png");
mainFrame.setIconImage(icon.getImage());
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
}
/**
* Hier werden die Informationen als String aus der XML herausgezogen.
* @param INeed
* @param forecast
* @return Jeweilige Infos, die dann in die GUI kommen.
*/
private static String getInformationFor(String INeed, String forecast) {
AuxMethods auxMethods = new AuxMethods();
String res = "";
switch(INeed) {
case "DATEOF": res = forecast.substring(forecast.indexOf("<dt_txt>")+8, forecast.indexOf("</dt_txt>"));
break;
case "DESCRIPTIONOF": res = forecast.substring(forecast.indexOf("<description>")+13, forecast.indexOf("</description>"));
break;
case "TEMPOF": res = forecast.substring(forecast.indexOf("<temp>")+6, forecast.indexOf("</temp>"));
res = auxMethods.kelvinToCelsius(Double.parseDouble(res));
break;
case "HUMIDITYOF": res = forecast.substring(forecast.indexOf("<humidity>")+10, forecast.indexOf("</humidity>"));
break;
case "PRESSUREOF": res = forecast.substring(forecast.indexOf("<pressure>")+10, forecast.indexOf("</pressure>")) + " hPa";
break;
case "WINDSPEEDOF": res = forecast.substring(forecast.indexOf("speed>")+6, forecast.indexOf("</speed>"));
double d = Double.parseDouble(res);
d = Math.round(d*1.61);
res = String.valueOf(d) + " km/h";
break;
default: res = "";
}
return res;
}
/**
* Hier werden die Daten per Rest ausgelesen sodass diese als JSONObjekt weiterverarbeitet werden können.
* @param ort
* @return JSONObject Die Wetterdaten.
*/
private static JSONObject readService(String ort) {
Client client = Client.create();
WebResource webResource = client
.resource("https://api.openweathermap.org/data/2.5/forecast?q="+ort+",De&APPID=ca9d175f389c46b262b56ef51ef64436");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
JSONObject json = new JSONObject(output);
return json;
}
}