How to identify controls in an SWT application uniquely? Some Controls may be inactive at first but later activated. Is there a way to identify and assign unique ids to all the controls?
Asked
Active
Viewed 267 times
2 Answers
2
You can associate arbitrary data with a control using the setData
and getData
methods:
control.setData("id key", "control id");
String id = (String)control.getData("id key");

greg-449
- 109,219
- 232
- 102
- 145
-
This method of setting and retrieving data works fine and runs perfectly when invoked at the beginning of the application, is there a generic way to identify uniquely, the controls created at runtime(like a popup shell)? – Sur Mar 30 '15 at 09:20
-
There is no generic way, you have to code it yourself. Normally it is not necessary to do this. – greg-449 Mar 30 '15 at 09:21
-
okay, I've coded it till the setting of IDs at the beginning of the application, but is there a way to get alerts on the generation of a new shell? Like a notification when some part of the code creates a new shell ? – Sur Mar 30 '15 at 09:27
-
You can use `Display.addFilter` to listen for `SWT.Activate` events to see new shells, but the controls in the shell probably haven't been created at that point. – greg-449 Mar 30 '15 at 09:47
-
Alright ! I'm trying to check if a widget has an ID or is null, if it's null then i'm calling setData on it again, you're right, the controls in the shell won't be activated if i call display.addFilter on SWT.Activate – Sur Mar 30 '15 at 09:50
1
Every SWT widget (org.eclipse.swt.widgets.Widget
) has:
getData
public Object getData(String key)
Returns the application defined property of the receiver with the specified name, or null if it has not been set. Applications may have associated arbitrary objects with the receiver in this fashion. If the objects stored in the properties need to be notified when the widget is disposed of, it is the application's responsibility to hook the Dispose event on the widget and do so.
You can use this is conjunction with public void setData(String key, Object value)

Diego Freniche
- 5,225
- 3
- 32
- 45
-
This way i can only get and set the ids at the beginning of the application, what if i need to set the ID of the controls that are generated at runtime like a popup shell ? – Sur Mar 30 '15 at 09:19
-
-
the keys are inactive at the start of the application and may get activated at runtime based on inputs. How can I programmatically know if a new Control has been created so that i can set the ID at runtime Or call setData()? – Sur Mar 30 '15 at 09:23
-
Just read the doc I posted: getData() Returns the application defined property of the receiver with the specified name, __or null if it has not been set__. If you get null you haven't set this property – Diego Freniche Mar 30 '15 at 09:25
-
okay! so if i get null then probably i'll have to set the data with the key,value pair !! Alright , got it .. thanks a lot :) – Sur Mar 30 '15 at 09:29