Skip to content

Instantly share code, notes, and snippets.

@lofidewanto
Created May 14, 2018 15:10
Show Gist options
  • Save lofidewanto/77fa30f220f9ec54cdecf86474e6bc4b to your computer and use it in GitHub Desktop.
Save lofidewanto/77fa30f220f9ec54cdecf86474e6bc4b to your computer and use it in GitHub Desktop.
/**
* A composite of a TextBox and a CheckBox that optionally enables it.
*/
public class OptionalTextBox extends Composite implements
ClickHandler {
private TextBox textBox = new TextBox();
private CheckBox checkBox = new CheckBox();
/**
* Constructs an OptionalTextBox with the given caption on the check.
*
* @param caption the caption to be displayed with the check box
*/
public OptionalTextBox(String caption) {
// Place the check above the text box using a vertical panel.
VerticalPanel panel = new VerticalPanel();
panel.add(checkBox);
panel.add(textBox);
// Set the check box's caption, and check it by default.
checkBox.setText(caption);
checkBox.setChecked(true);
checkBox.addClickHandler(this);
// All composites must call initWidget() in their constructors.
initWidget(panel);
// Give the overall composite a style name.
setStyleName("example-OptionalCheckBox");
}
public void onClick(ClickEvent event) {
Object sender = event.getSource();
if (sender == checkBox) {
// When the check box is clicked, update the text box's enabled state.
textBox.setEnabled(checkBox.isChecked());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment