There are two ways to do this:
- Make the
Label
a field of the class - Make the
Label
final
Here is an example:
private Label fieldLabel = new Label(shell, SWT.NONE);
public void testMethod()
{
Button button = new Button(shell, SWT.PUSH);
button.setText("Print");
final Label finalLabel = new Label(shell, SWT.NONE);
button.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event e)
{
fieldLabel.setText("TEXT");
finalLabel.setTexT("TEXT");
}
})
}
This is nothing specific to SWT, so you might want to read something about closure in programming.
Here is a related question on SO where the infamous Jon Skeet posted an answer...