By performing this task, you are creating the bean that holds the state and city data that you can use with the comboboxes.
Expand the plotCity > Source Packages nodes.
Right-click the plotCity package and choose New > Java class.
Type StateBean for the Class Name, plotCity for the Package and click Finish.
Add the following variable declarations to the class:
private String[] states =
new String [] {"Alaska", "Arizona", "California", "Oregon"};
private String[] stateCodes =
new String[]{"AK", "AZ", "CA", "OR"};
protected String[] AKCities =
new String[] {"Anchorage", "Fairbanks", "Juneau", "Nome"};
private ResourceBundle cityNames = null;
Add an init method after the constructor that creates a ResourceBundle object from the cities.properties file:
private void init() {
cityNames = ResourceBundle.getBundle("plotCity.cities");
}
Add a call to the init method inside the StateBean constructor:
public StateBean(){
this.init();
}
Add the following getStates method to the class. It loads a JSON array with the list of state names that you initialized in step 4:
public String getStates() throws JSONException {
JSONArray statesData = new JSONArray();
JSONObject stateData = new JSONObject();
for (int loop = 0; loop < states.length; loop++) {
stateData.put("label", states[loop]);
stateData.put("value", stateCodes[loop]);
statesData.put(stateData);
stateData = new JSONObject();
}
return jmaki.util.JSONUtil.jsonArrayToString(statesData, new StringBuffer());
}
Add the following getCities method to the class. It loads a JSON array with the list of Alaskan city names that you initialized in step 4:
public String getCities() throws Exception {
JSONObject cityData = new JSONObject();
JSONArray citiesData = new JSONArray();
for(int i = 0; i < AKCities.length; i++){
cityData.put("label", AKCities[i]).toString();
cityData.put("value", AKCities[i]).toString();
citiesData.put(cityData);
cityData = new JSONObject();
}
return jmaki.util.JSONUtil.jsonArrayToString(citiesData, new StringBuffer());
}
Add the following getNewCities method to the class. It loads a JSON array with the list of city names for the selected state:
public String getNewCities(String state) throws JSONException {
JSONObject city = new JSONObject();
JSONArray cities = new JSONArray();
String[] names = null;
try {
names = cityNames.getString(state).split(",");
} catch(Exception e){
return null;
}
for(int i = 0; i < names.length; i++){
city.put("label", names[i]);
city.put("value", names[i]);
cities.put(city);
city = new JSONObject();
}
return jmaki.util.JSONUtil.jsonArrayToString(cities, new StringBuffer());
}
Right-click the editor pane and select Fix Imports from the pop-up menu. The IDE will import the packages the class needs.
Save StateBean.java