Getting Started With Project jMaki for the GlassFish v3 Application Server

ProcedureCreating a JavaBeans Component That Holds the Data

By performing this task, you are creating the bean that holds the state and city data that you can use with the comboboxes.

  1. Expand the plotCity > Source Packages nodes.

  2. Right-click the plotCity package and choose New > Java class.

  3. Type StateBean for the Class Name, plotCity for the Package and click Finish.

  4. 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;
  5. 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");   
    }
  6. Add a call to the init method inside the StateBean constructor:

    public StateBean(){
    	this.init();
    }
  7. 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());
    }
  8. 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());
    }
  9. 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());
    }
  10. Right-click the editor pane and select Fix Imports from the pop-up menu. The IDE will import the packages the class needs.

  11. Save StateBean.java