2

I was wondering if there's any way to get all the widgets in a given Shell, including widgets that are grandchildren of Shell. What's the easiest way to accomplish this?

devoured elysium
  • 101,373
  • 131
  • 340
  • 557

2 Answers2

2

A little bit more readable and extended than the only answer:

The main idea is that you cannot get all widgets "at once", you need to use matcher. Moreover, even when you've got the widgets, you should create a copy of them to avoid Invalid Thread Access error.

So, the snippet for matching all (in this case, all Text widgets, but it can be easily adopted for any type, or Widget, as in Kane's answer):

import org.eclipse.swtbot.swt.finder.SWTBot;
import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotText;

Matcher<? extends Text> matcher = WidgetMatcherFactory.widgetOfType(Text.class);
List<? extends Text> widgets = new SWTBot().widgets(matcher);

for (Text t : widgets) {
  // Create a copy to work with to avoid Invalid Thread exception
  SWTBotText text = new SWTBotText (t);
  // Do stuff...
}
The Godfather
  • 4,235
  • 4
  • 39
  • 61
1

Try org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf(WidgetMatcherFactory.widgetOfType(Widget.class))

Kane
  • 8,035
  • 7
  • 46
  • 75