I have such group:
leftPanel = new Group(subParentContainer, SWT.NONE);
leftPanel.setLayout(new RowLayout(SWT.VERTICAL));
leftPanel.setText("Group title");
Button firstTypeBtn = new Button(leftPanel, SWT.RADIO);
Button secondTypeBtn = new Button(leftPanel, SWT.RADIO);
Button thirdTypeBtn = new Button(leftPanel, SWT.RADIO);
firstTypeBtn.setText("Text 1");
firstTypeBtn.setData(myEnum.ONE);
firstTypeBtn.addListener(SWT.Selection, lst);
secondTypeBtn.setText("Text 2");
secondTypeBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
fetchData(1);
}
});
thirdTypeBtn.setText("Text 3");
thirdTypeBtn.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
fetchData(2);
}
});
leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
as you can see my buttons have similar code for click processing. I would like to make one common variable which will handle click events. But I don't know how this single listener will recognise which button was clicked. As I see button widget has such method .setDate(Object object)
this is the only one method which I think can help me. So I did one single listener:
Listener lst = new Listener() {
@Override
public void handleEvent(Event event) {
System.out.println((Button) event.data);
}
};
and one enum which will help with button recognising:
public enum myEnum {
ONE, TWO, THREE;
}
What I think I will do with these code - I will assign enum to each button and then in one listener will recognise them. As you can see in code scope above I tried to add such solution to the first button. But when I pressed on it I saw in my console only: null
. I have two variants why my solution doesn't work: I did it in wrong way or my idea is totaly impossible. Maybe someone on this website did smth similar and will be able to help me ?
The data
field of the Event
does not contain the data from the button. It's contents depends on the exact event but it is not set for a button selection.
The widget
field of Event
contains the control (or widget) generating the event. So you can get the data with:
event.widget.getData();