table.add(new Column("name")).setSortable(true); form.add(new TextField("firstname")).setValue("Bob");If you created a custom Form, FieldSet or Table and overrode the add or addColumn method, you will be forced to return the methods argument. For example if you had:
public class MyTable { public void addColumn(Column column) { ... } }you will need to update as follows:
public class MyTable { public Column addColumn(Column column) { ... // You must return the column return column; } }
public class HtmlTable extends AbstractControl { ... public String toString() { int estimatedControlSize = 1000; HtmlStringBuffer buffer = new HtmlStringBuffer(estimatedControlSize); // Rendering Start buffer.elementStart("table"); appendAttributes(buffer); buffer.elementClose(); renderRows(buffer); buffer.closeElement("table"); // Rendering End return buffer.toString(); } }to this:
public class HtmlTable extends AbstractControl { ... public void render(HtmlStringBuffer buffer) { // Rendering Start buffer.elementStart("table"); appendAttributes(buffer); buffer.elementClose(); renderRows(buffer); buffer.closeElement("table"); // Rendering End } public String toString() { int estimatedControlSize = 1000; HtmlStringBuffer buffer = new HtmlStringBuffer(estimatedControlSize); render(buffer); return buffer.toString(); } }Note, the code between the commented section, was moved from toString to the render method. Also note that invoking a Control's toString() method still outputs the same HTML representation, as toString() delegates to the render method. Please note a common problem when overriding render in custom components, is invoking super.toString() in order to render the Control's default markup:
public class CustomField extends Field {
...
public void render(HtmlStringBuffer buffer) {
String field = super.toString(); // BEWARE this line will cause StackOverflowError
...
}
public String toString() {
HtmlStringBuffer buffer = new HtmlStringBuffer();
render(buffer);
return buffer.toString();
}
}
The highlighted line above will cause a StackOverflowError, meaning an infinite
loop was encountered. The reason for the error will become obvious when
tracing the sequence of calls:
public class CustomField extends Field {
...
public void render(HtmlStringBuffer buffer) {
super.render(buffer); // NOTE StackOverflowError won't occur
...
}
public String toString() {
HtmlStringBuffer buffer = new HtmlStringBuffer();
render(buffer);
return buffer.toString();
}
}
<click-app charset="UTF-8"> <pages package="net.sf.click.examples.page"/> <log-service classname="net.sf.click.extras.service.JdkLogService"/> </click-app>While it is generally not recommended to use the Click LogService in your application code, you can retrieve it using the ClickUtils method getLogService().
public class MyPage extends Page { public void onInit() { Submit submit = new Submit("submit"); submit.setActionListener(new ActionListener() { public boolean onAction(Control source) { // Perform submit action and return true to continue processing return true; } }); } }Note that the source Control is passed in as an argument to the onAction method. For very complex scenarios it is useful to know which Control triggered the event [369].