ilmash
ilmash

Reputation: 181

Java SWT: Widgets(buttons, labels) used properly

It's my first question here but I'm really stuck. Maybe it's just my extreme fatigue for last few days, but I looked up google for few hours now and couldn't find any close to good answer.

I know SWT is event driven like all GUI's I can think of and when creating widgets I should keep in mind that they need to be able to reach the ones they are supposed to modify/interact with. But what I need to know is if my thinking is right and if not what should I improve.

Let's say I start with eclipse+windowbuilder+swt/jface java project. Then I add button and clabel +click listener for button(SelectionListener), so the generated code looks more or less like(only main method, above there is only Main class and imports)

public static void main(String[] args) {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
        }
    });
    btnNewButton.setBounds(51, 31, 75, 25);
    btnNewButton.setText("New Button");

    CLabel lblOneTwo = new CLabel(shell, SWT.NONE);
    lblOneTwo.setBounds(180, 119, 61, 21);
    lblOneTwo.setText("one two");

    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

As I know and it's probably obvious to most of you I can't just go and add to

btnNewButton.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    }
});

code like lblOneTwo.setText("three") and from what I know and use sometimes I just declare all stuff beforehand, as static widget widgetName and then I can access them from basically everywhere, so code like

static Button btnNewButton;
static CLabel lblOneTwo;

public static void main(String[] args) {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            lblOneTwo.setText("three");
        }
    });
    (...)

works just fine. But I guess and think it's not the best practice and way of doing it, isn't it? So please, help me, point me in the right direction so I can stop coding in a sin. Thanks in advance! indirect answers with links to articles/tutorials would be great, but I like examples people are putting here because of their clear way of showing things.

Upvotes: 0

Views: 561

Answers (1)

Baz
Baz

Reputation: 36884

There are multiple ways to do this.

  1. Make the widgets field of your class. Don't make them static unless necessary.

    private Label l;
    private Button b;
    public static void main(String[] args)
    {
        ...
        b.addListener(...);
    
  2. Define all your widgets first (have to be final if you want to use them in the Listener), after that add your Listeners.

    final Label l = ...;
    final Button b = ....;
    b.addListener(...);
    
  3. If you want to change the widget itself in the Listener you can use Event#widget to get the source of the event.

    b.addListener(SWT.Selection, new Listener()
    {
        public void handleEvent(Event e)
        {
            Button button = (Button) e.widget;
        }
    });
    

Upvotes: 1

Related Questions