on May 8th, 2009UI.toString() in a snippet
Ever wanted to get a print out of all components that make up a piece of UI?
This little recursive snippet does that for you.
private void printChildren(Composite composite, int count) {
StringBuilder spaces = new StringBuilder(count * 2);
for (int i = 0; i < count * 2; i++) {
spaces.append(' ');
}
for (Control c : composite.getChildren()) {
System.out.println(String.format("%s%s (%s)", spaces.toString(), c.toString(), c.getLayoutData()));
if (c instanceof Composite) {
printChildren((Composite) c, count + 1);
}
}
}
Here’s an example of the output:
Composite {} (null)
Composite {} (null)
Composite {} (null)
Composite {} (GridData {horizontalAlignment=Undefined 256 grabExcessHorizontalSpace=true verticalAlignment=GridData.CENTER})
Label {Subject:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Label {This is a message about the cool Eclipse RCP!} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Label {From:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Link {<a>nicole@mail.org</a>} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Label {Date:} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Label {10:34 am} (GridData {horizontalAlignment=SWT.BEGINNING verticalAlignment=GridData.CENTER})
Text {} (GridData {horizontalAlignment=SWT.FILL grabExcessHorizontalSpace=true verticalAlignment=SWT.FILL grabExcessVerticalSpace=true})
Related posts:
- Enhanced JavaScript Bridge, Eclipse Galileo Feature #9
- Replacing the Perspective-Switcher in RCP apps
- Tracing Keybindings in Eclipse RCP
- Tip: Spellchecking in Eclipse




Hi Elias,
do you know about PDE Picasso? It helps you to find layout issues by coloring all widgets and in addition it provides tooltips on every widgets with layout issues and other informations of the widget.
See http://wiki.eclipse.org/PDE/Incubator/Picasso
And also ping Chris regarding https://bugs.eclipse.org/bugs/show_bug.cgi?id=267975
Hi Ben,
thanks, I know about picasso
.
Greetings,
Elias.