Skip to content

Instantly share code, notes, and snippets.

@roberto-filho
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save roberto-filho/c240435d69d99fadee40 to your computer and use it in GitHub Desktop.

Select an option

Save roberto-filho/c240435d69d99fadee40 to your computer and use it in GitHub Desktop.
Bind a "method" to a button through reflection
protected void bindAction(Button btn, final String methodName) {
try {
final Method method = this.getClass().getMethod(methodName, new Class[] {});
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
method.invoke(GermantechEditor.this, new Object[] {});
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
} catch (Exception e1) {
throw new RuntimeException("Não foi possível encontrar o método \"" + methodName + "\"", e1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment