Weather Information App (JavaFX)
Java Source Code
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class WeatherApp extends Application {
// Insert your OpenWeatherMap API key here
private static final String API_KEY = "235ce29f3b4c0dcdd65938edbb52ea9a";
private TextField cityField;
private TextArea weatherDisplay;
private ListView<String> historyList;
private ComboBox<String> unitBox;
private BorderPane root;
@Override
public void start(Stage stage) {
root = new BorderPane();
cityField = new TextField();
[Link]("Enter city name");
Button searchBtn = new Button("Search Weather");
Button locationBtn = new Button("Use Current Location");
Button helpBtn = new Button("Help / README");
unitBox = new ComboBox<>();
[Link]().addAll("Celsius", "Fahrenheit");
[Link]("Celsius");
weatherDisplay = new TextArea();
[Link](false);
historyList = new ListView<>();
HBox topBar = new HBox(10, cityField, searchBtn, locationBtn, helpBtn, unitBox);
[Link](new Insets(10));
[Link](topBar);
[Link](weatherDisplay);
[Link](historyList);
[Link](e -> searchWeather());
[Link](e -> getWeatherByLocation());
[Link](e -> showReadme());
Scene scene = new Scene(root, 850, 520);
[Link]("Weather Information App");
[Link](scene);
[Link]();
}
private void searchWeather() {
try {
String city = [Link]();
if ([Link]()) {
showError("Please enter a city name.");
return;
}
String units = [Link]().equals("Celsius") ? "metric" : "imperial";
String url = "[Link]
+ city + "&units=" + units + "&appid=" + API_KEY;
String response = getResponse(url);
displayWeather(response, city);
} catch (Exception e) {
showError("Error fetching weather data.");
}
}
private void getWeatherByLocation() {
try {
String locationUrl = "[Link]
String locationResponse = getResponse(locationUrl);
JSONObject locationData = new JSONObject(locationResponse);
double lat = [Link]("lat");
double lon = [Link]("lon");
String city = [Link]("city");
String units = [Link]().equals("Celsius") ? "metric" : "imperial";
String weatherUrl = "[Link]
+ lat + "&lon=" + lon + "&units=" + units + "&appid=" + API_KEY;
String weatherResponse = getResponse(weatherUrl);
displayWeather(weatherResponse, city);
} catch (Exception e) {
showError("Unable to detect location.");
}
}
private void displayWeather(String response, String city) {
JSONObject data = new JSONObject(response);
double temp = [Link]("main").getDouble("temp");
int humidity = [Link]("main").getInt("humidity");
double wind = [Link]("wind").getDouble("speed");
String condition = [Link]("weather")
.getJSONObject(0).getString("description");
String result = "City: " + city + "\n"
+ "Temperature: " + temp + "\n"
+ "Humidity: " + humidity + "%\n"
+ "Wind Speed: " + wind + "\n"
+ "Condition: " + condition + "\n";
[Link](result);
addToHistory(city);
updateBackground();
}
private void updateBackground() {
int hour = [Link]().getHour();
if (hour >= 6 && hour < 18) {
[Link]("-fx-background-color: lightblue;");
} else {
[Link]("-fx-background-color: darkslateblue;");
}
}
private void addToHistory(String city) {
String time = [Link]()
.format([Link]("yyyy-MM-dd HH:mm"));
[Link]().add(city + " - " + time);
}
private void showReadme() {
Alert readme = new Alert([Link]);
[Link]("README - Weather Information App");
[Link]("How to Use the Weather Information App");
[Link](
"Overview:\n"
+ "This Weather Information App retrieves real-time weather data "
+ "using the OpenWeatherMap API.\n\n"
+ "Features:\n"
+ "- Search weather by city name\n"
+ "- Detect weather using your current location\n"
+ "- Temperature unit conversion (Celsius / Fahrenheit)\n"
+ "- Displays temperature, humidity, wind speed, and condition\n"
+ "- Search history with timestamps\n"
+ "- Dynamic background based on time of day\n\n"
+ "How to Use:\n"
+ "1. Enter a city name and click 'Search Weather'.\n"
+ "2. Or click 'Use Current Location'.\n"
+ "3. Select the temperature unit.\n"
+ "4. Weather results will appear on the screen.\n\n"
+ "API Used:\n"
+ "OpenWeatherMap API\n"
+ "[Link]
+ "Developer:\n"
+ "Weather Information App Assignment"
);
[Link]();
}
private void showError(String message) {
Alert alert = new Alert([Link]);
[Link]("Error");
[Link](message);
[Link]();
}
private String getResponse(String urlString) throws Exception {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) [Link]();
[Link]("GET");
BufferedReader reader = new BufferedReader(
new InputStreamReader([Link]()));
String line;
StringBuilder response = new StringBuilder();
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
return [Link]();
}
public static void main(String[] args) {
launch();
}
}