Java Reference
In-Depth Information
monthDDM.append(String.valueOf(ctr));
monthDDM.append("</OPTION>");
}
monthDDM.append("</SELECT>");
this .monthDDM = String.valueOf(monthDDM);
return this .monthDDM;
}
public static void main(String[] args) {
MonthBean mb = new MonthBean();
System.out.println(mb.getMonthDDM());
}
}
Notice that MonthBean has a null constructor and one property, monthDDM, which can only be retrieved.
In addition, the getter should look familiar because the logic is the same as the MonthCh class (in the client-based
application). The difference is that the getter returns HTML that defines a Select component (i.e., drop-down menu),
whereas MonthCh was a subclass of Choice . Notice also that MonthBean has two variables named monthDDM. One
is a string and the other is a string buffer. The string buffer is used to build the HTML because the append function
makes it much easier to manipulate the characters. However, the HTML must be returned as a string. Therefore, the
string buffer's final value is converted to a string, and the class String variable monthDDM is returned. Verify that
MonthBean works correctly by running the class as a Java application. The main method contains code that will create
a MonthBean object and then display the text returned by getMonthDDM. Verify that the HTML returned matches the
HTML shown earlier.
In the rest of the exercise, you will create beans that perform the same functions as the remaining time and date
choice components from the client-based application.
4.
In Java Resources/src/c9, create a new Java class called DayBean.
5.
Change the DayBean source to the following:
package c9;
import java.util.Calendar;
public class DayBean {
public String dayDDM = new String();
private int currDay;
private Calendar c = Calendar.getInstance();
public DayBean() {}
public String getDayDDM() {
currDay = c.get(Calendar. DAY_OF_MONTH);
StringBuffer dayDDM =
new StringBuffer("<SELECT name='DayDDM'>");
for ( int ctr = 1; ctr <= 31; ctr++) {
dayDDM.append("<OPTION value='");
dayDDM.append(String.valueOf(ctr));
if (ctr == currDay) {
dayDDM.append("' selected>");
} else {
dayDDM.append("'>");
}
dayDDM.append(String.valueOf(ctr));
dayDDM.append("</OPTION>");
}
Search WWH ::




Custom Search